Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class WorldInteraction : MonoBehaviour
- {
- UnityEngine.AI.NavMeshAgent playerAgent;
- Animator playerAnim;
- private bool walking;
- void Start()
- {
- playerAnim = GetComponent<Animator>();
- playerAgent = GetComponent<UnityEngine.AI.NavMeshAgent>();
- }
- void Update()
- {
- // Check for Mouse Button Down for Interaction
- // Check for Pointer hover UI or GameObject
- // If hover UI then dont set Ray to World
- if (Input.GetMouseButtonDown(0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
- GetInteraction();
- Debug.DrawRay(transform.position, transform.forward * 5f, Color.red);
- }
- void GetInteraction()
- {
- // Send ray from MousePoint (ScreenPointToRay)
- // store the interaction information
- // store the interacted object information
- Ray interactionRay = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit interactionInfo;
- if (Physics.Raycast(interactionRay, out interactionInfo, Mathf.Infinity))
- {
- // declares that interaction object is = to collider from gameObject from interactionray information
- GameObject interactionObject = interactionInfo.collider.gameObject;
- if (interactionObject.tag == "Enemy")
- {
- interactionObject.GetComponent<Interactable>().MoveToInteraction(playerAgent);
- }
- else
- {
- // generate a path form player to the target
- playerAgent.destination = interactionInfo.point;
- walking = true;
- }
- }
- playerAnim.SetBool("isWalking", walking);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement