mirror of
				https://github.com/DerTyp7/harvestdale-unity.git
				synced 2025-10-31 05:27:07 +01:00 
			
		
		
		
	interactable
This commit is contained in:
		
							
								
								
									
										53
									
								
								Assets/Scripts/UI/CursorText.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								Assets/Scripts/UI/CursorText.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,53 @@ | ||||
| // Text which is left from the cursor | ||||
| using UnityEngine; | ||||
| using TMPro; | ||||
|  | ||||
| public class CursorText : MonoBehaviour | ||||
| { | ||||
|   private TextMeshProUGUI textComponent; | ||||
|   private string text = ""; | ||||
|   private GameObject player; | ||||
|  | ||||
|   private void Start() | ||||
|   { | ||||
|     textComponent = GetComponent<TextMeshProUGUI>(); | ||||
|     player = GameObject.FindGameObjectWithTag("Player"); | ||||
|   } | ||||
|  | ||||
|   public void SetText(string newText) | ||||
|   { | ||||
|     text = newText; | ||||
|     this.textComponent.SetText(text); | ||||
|   } | ||||
|  | ||||
|   private void Update() | ||||
|   { | ||||
|     if (text.Length > 0) | ||||
|     { | ||||
|       // Set gameobject left from cursor | ||||
|       Vector3 mousePos = Input.mousePosition; | ||||
|       mousePos.x += 125; | ||||
|       transform.position = mousePos; | ||||
|     } | ||||
|  | ||||
|  | ||||
|     // if hovering over interactable object | ||||
|     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); | ||||
|     RaycastHit2D hit; | ||||
|  | ||||
|     hit = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity, LayerMask.GetMask("Interactable")); | ||||
|  | ||||
|     if (hit.collider != null) | ||||
|     { | ||||
|       Interactable interactable = hit.collider.GetComponent<Interactable>(); | ||||
|       if (interactable != null) | ||||
|       { | ||||
|         SetText(interactable.GetInteractText(player)); | ||||
|       } | ||||
|     } | ||||
|     else | ||||
|     { | ||||
|       SetText(""); | ||||
|     } | ||||
|   } | ||||
| } | ||||
							
								
								
									
										11
									
								
								Assets/Scripts/UI/CursorText.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Assets/Scripts/UI/CursorText.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: 247a0445b7ab3a14badc0fce020c3bc5 | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   defaultReferences: [] | ||||
|   executionOrder: 0 | ||||
|   icon: {instanceID: 0} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
		Reference in New Issue
	
	Block a user
	 Janis
					Janis