mirror of
				https://github.com/DerTyp7/example-top-down-unity.git
				synced 2025-10-30 04:47:09 +01:00 
			
		
		
		
	Interaction
This commit is contained in:
		
							
								
								
									
										50
									
								
								Assets/Scripts/Interactable.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								Assets/Scripts/Interactable.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,50 @@ | ||||
| using UnityEngine; | ||||
|  | ||||
| // Represents the base class for all interactable objects in the scene | ||||
| public abstract class Interactable : MonoBehaviour | ||||
| { | ||||
|  | ||||
|     public enum InteractionType { | ||||
|         Click, | ||||
|         Hold | ||||
|     } | ||||
|      | ||||
|     float holdTime; | ||||
|     Transform playerTransform; | ||||
|     Transform interactableTransform; | ||||
|  | ||||
|     public InteractionType interactionType; | ||||
|     public float radius = 3f; | ||||
|  | ||||
|     public abstract string GetDescription(); | ||||
|     public abstract void Interact(); | ||||
|  | ||||
|     public void IncreaseHoldTime() => holdTime += Time.deltaTime; | ||||
|     public void ResetHoldTime() => holdTime = 0f; | ||||
|  | ||||
|     public float GetHoldTime() => holdTime; | ||||
|      | ||||
|     public bool isInRange() | ||||
|     { | ||||
|         playerTransform = GameObject.FindGameObjectWithTag("Player").gameObject.transform; // Maybe singleton later? | ||||
|         interactableTransform = gameObject.transform; | ||||
|         float distance = Vector2.Distance(interactableTransform.position, playerTransform.position); | ||||
|  | ||||
|         if(distance <= radius) | ||||
|         { | ||||
|             return true; | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             return false; | ||||
|         } | ||||
|  | ||||
|     } | ||||
|  | ||||
|     private void OnDrawGizmosSelected() | ||||
|     { | ||||
|         // Gizmos are only visible in the scene view -> NOT visible IN-GAME (DEBUG Reasons) | ||||
|         Gizmos.color = Color.magenta; | ||||
|         Gizmos.DrawWireSphere(transform.position, radius); | ||||
|     } | ||||
| } | ||||
| @@ -1,5 +1,5 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: 3367b6118f93acb46b35bdeef787e45d | ||||
| guid: 185b418881f2566459c0cffeb5dfc0bd | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
| @@ -1,12 +0,0 @@ | ||||
| using System.Collections; | ||||
| using System.Collections.Generic; | ||||
| using UnityEngine; | ||||
|  | ||||
| /* | ||||
|  Base Class for all objects with interaction | ||||
|  */ | ||||
|  | ||||
| public class InteractableObject : MonoBehaviour | ||||
| { | ||||
|  | ||||
| } | ||||
							
								
								
									
										11
									
								
								Assets/Scripts/PlayerController.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Assets/Scripts/PlayerController.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| using System.Collections; | ||||
| using System.Collections.Generic; | ||||
| using UnityEngine; | ||||
|  | ||||
| // Handles the player input, such as interactions | ||||
| public class PlayerController : MonoBehaviour | ||||
| { | ||||
|     void Update() | ||||
|     { | ||||
|     } | ||||
| } | ||||
							
								
								
									
										11
									
								
								Assets/Scripts/PlayerController.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Assets/Scripts/PlayerController.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: 194170eed9e58d74584e7e11747aea0b | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   defaultReferences: [] | ||||
|   executionOrder: 0 | ||||
|   icon: {instanceID: 0} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
							
								
								
									
										69
									
								
								Assets/Scripts/PlayerInteraction.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										69
									
								
								Assets/Scripts/PlayerInteraction.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,69 @@ | ||||
| using System.Collections; | ||||
| using System.Collections.Generic; | ||||
| using UnityEngine; | ||||
|  | ||||
| public class PlayerInteraction : MonoBehaviour | ||||
| { | ||||
|     public TMPro.TextMeshProUGUI interactionText; | ||||
|  | ||||
|     void Update() | ||||
|     { | ||||
|         bool successfulHit = false; | ||||
|         RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); | ||||
|  | ||||
|         if(hit.collider != null) | ||||
|         { | ||||
|             Interactable interactable = hit.collider.gameObject.GetComponent<Interactable>(); | ||||
|  | ||||
|             if(interactable != null) | ||||
|             { | ||||
|                 // Debug.Log("Target Position: " + hit.collider.gameObject.transform.position); | ||||
|                 HandleInteraction(interactable); | ||||
|                 interactionText.text = interactable.GetDescription(); | ||||
|                 successfulHit = true; | ||||
|             }             | ||||
|         } | ||||
|  | ||||
|         if (!successfulHit) | ||||
|         { | ||||
|             interactionText.text = ""; | ||||
|         } | ||||
|  | ||||
|     } | ||||
|  | ||||
|     void HandleInteractionText() // interaction text has to follow mouse cursor | ||||
|     { | ||||
|          | ||||
|     } | ||||
|  | ||||
|     void HandleInteraction(Interactable interactable) | ||||
|     { | ||||
|         switch (interactable.interactionType) | ||||
|         { | ||||
|             case Interactable.InteractionType.Click: | ||||
|                 if (Input.GetButtonDown("Interact")) | ||||
|                 { | ||||
|                     interactable.Interact(); | ||||
|                 } | ||||
|                 break; | ||||
|             case Interactable.InteractionType.Hold: | ||||
|                 if (Input.GetButton("Interact")) | ||||
|                 { | ||||
|                     interactable.IncreaseHoldTime(); | ||||
|                      | ||||
|                     if(interactable.GetHoldTime() > 1f) | ||||
|                     { | ||||
|                         interactable.Interact(); | ||||
|                         interactable.ResetHoldTime(); | ||||
|                     } | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     interactable.ResetHoldTime(); | ||||
|                 } | ||||
|                 break; | ||||
|             default: | ||||
|                 throw new System.Exception("Unsupported type of interactable"); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										11
									
								
								Assets/Scripts/PlayerInteraction.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Assets/Scripts/PlayerInteraction.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: c17c7c66da95ea747b93edf1da56ba16 | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   defaultReferences: [] | ||||
|   executionOrder: 0 | ||||
|   icon: {instanceID: 0} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
| @@ -2,25 +2,37 @@ using System.Collections; | ||||
| using System.Collections.Generic; | ||||
| using UnityEngine; | ||||
|  | ||||
| // Handles the player's movement, such as running, walking, sneaking  | ||||
| public class PlayerMovement : MonoBehaviour | ||||
| { | ||||
| {    | ||||
|     // Speed multiplier of the movement | ||||
|     [SerializeField] float moveSpeed = 5f; | ||||
|  | ||||
|     [SerializeField] Rigidbody2D rb; | ||||
|     [SerializeField] Animator animator; | ||||
|  | ||||
|     Rigidbody2D rb; | ||||
|     Animator animator; | ||||
|     Vector2 movement; | ||||
|     private void Update() | ||||
|      | ||||
|     void Start() | ||||
|     { | ||||
|         rb = GetComponent<Rigidbody2D>(); | ||||
|         animator = GetComponent<Animator>(); | ||||
|     } | ||||
|  | ||||
|     void Update() | ||||
|     { | ||||
|         movement.x = Input.GetAxisRaw("Horizontal"); | ||||
|         movement.y = Input.GetAxisRaw("Vertical"); | ||||
|  | ||||
|         // Normalize, so the player is not faster by moving diagonally | ||||
|         movement.Normalize(); | ||||
|  | ||||
|         animator.SetFloat("Horizontal", movement.x); | ||||
|         animator.SetFloat("Vertical", movement.y); | ||||
|         animator.SetFloat("Speed", movement.sqrMagnitude); | ||||
|     } | ||||
|  | ||||
|     private void FixedUpdate() | ||||
|     void FixedUpdate() | ||||
|     { | ||||
|         if (rb != null) | ||||
|         { | ||||
|   | ||||
							
								
								
									
										14
									
								
								Assets/Scripts/TreeInteraction.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								Assets/Scripts/TreeInteraction.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,14 @@ | ||||
| using System.Collections; | ||||
| using System.Collections.Generic; | ||||
| using UnityEngine; | ||||
|  | ||||
| public class TreeInteraction : Interactable | ||||
| { | ||||
|     public override string GetDescription() => "Baum muss schreie"; | ||||
|     public override void Interact() | ||||
|     { | ||||
|         if(isInRange()) | ||||
|             Debug.Log("AaaaaaaaaaAaAAaAaAAaAaAaaaaaaaaaaaaaaaaaahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"); | ||||
|     } | ||||
| } | ||||
|  | ||||
							
								
								
									
										11
									
								
								Assets/Scripts/TreeInteraction.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Assets/Scripts/TreeInteraction.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: c68a9987090906346b5bc7622cb96556 | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   defaultReferences: [] | ||||
|   executionOrder: 0 | ||||
|   icon: {instanceID: 0} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
		Reference in New Issue
	
	Block a user
	 janis
					janis