SHARE
TWEET

WorldInteraction

a guest Feb 14th, 2020 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class WorldInteraction : MonoBehaviour
  5. {
  6.  
  7.     UnityEngine.AI.NavMeshAgent playerAgent;
  8.     Animator playerAnim;
  9.     private bool walking;
  10.     void Start()
  11.     {
  12.         playerAnim = GetComponent<Animator>();
  13.         playerAgent = GetComponent<UnityEngine.AI.NavMeshAgent>();
  14.        
  15.     }
  16.  
  17.     void Update()
  18.     {
  19.         //  Check for Mouse Button Down for Interaction
  20.         //  Check for Pointer hover UI or GameObject
  21.         //  If hover UI then dont set Ray to World
  22.         if (Input.GetMouseButtonDown(0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
  23.             GetInteraction();
  24.         Debug.DrawRay(transform.position, transform.forward * 5f, Color.red);
  25.     }
  26.  
  27.  
  28.     void GetInteraction()
  29.     {
  30.         // Send ray from MousePoint (ScreenPointToRay)
  31.         // store the interaction information
  32.         // store the interacted object information
  33.         Ray interactionRay = Camera.main.ScreenPointToRay(Input.mousePosition);
  34.         RaycastHit interactionInfo;
  35.         if (Physics.Raycast(interactionRay, out interactionInfo, Mathf.Infinity))
  36.         {
  37.             // declares that interaction object is = to collider from gameObject from interactionray information
  38.             GameObject interactionObject = interactionInfo.collider.gameObject;
  39.             if (interactionObject.tag == "Enemy")
  40.             {
  41.                 interactionObject.GetComponent<Interactable>().MoveToInteraction(playerAgent);
  42.             }
  43.             else
  44.             {
  45.                 // generate a path form player to the target
  46.                 playerAgent.destination = interactionInfo.point;
  47.                 walking = true;
  48.             }
  49.         }
  50.         playerAnim.SetBool("isWalking", walking);
  51.     }
  52. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top