Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.98 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5.  
  6. public class WorldInteraction : MonoBehaviour {
  7.     //declare variable here so it's avaibable to the whole class
  8.     NavMeshAgent playerAgent;
  9.  
  10.     void Start()
  11.     {
  12.         // When initiated, set playerAgent to NavMeshAgent (a component for pathfinding)
  13.         playerAgent = GetComponent<NavMeshAgent> (); //get the NavMeshAgent for the object this script is attatched to
  14.     }
  15.  
  16.     void Update () {                        //STOPS PLAYER FROM MOVING WHEN DIALOGUE PANEL IS ACTIVE
  17.         if (Input.GetMouseButtonDown (0) && !DialogueSystem.Instance.dialoguePanel.activeInHierarchy)
  18.         {
  19.             MoveCamera (); //call this when you click the mouse
  20.         }
  21.     }
  22.  
  23.     void MoveCamera()
  24.     {
  25.         // Think ray of sunshine
  26.         // Creates a Ray from the Main camera to the mouse position (when clicked)
  27.         Ray cameraRay = Camera.main.ScreenPointToRay (Input.mousePosition);
  28.  
  29.         //creates new RayCastHit object to get info from raycast
  30.         RaycastHit cameraInfo;
  31.  
  32.         // check for collisions in the Ray
  33.         // out means the arg has no value but will be defined by the method
  34.  
  35.         // Basically Physics.Raycast starts from the camera to the mouse
  36.         // It uses info from the Raycase (camera info) to check for any collisions
  37.         // to make a new game object with the object the Ray collided with
  38.         // If the object has an Interactive Object tag, it calls the Interactable script
  39.         // (which is a component) and then calls the MoveToInteraction method, passing the player
  40.         // in as an argument
  41.         if (Physics.Raycast (cameraRay, out cameraInfo, Mathf.Infinity))
  42.         {
  43.             GameObject interactedObject = cameraInfo.collider.gameObject;
  44.             if (interactedObject.tag == "Interactable Object") {
  45.                 //work with the Interactable script on this object
  46.                 interactedObject.GetComponent<Interactable> ().MoveToInteraction (playerAgent);
  47.             }
  48.             else
  49.             {
  50.                 playerAgent.stoppingDistance = 0;
  51.                 playerAgent.destination = cameraInfo.point;
  52.             }
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement