Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.84 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 () {
  17.         if (Input.GetMouseButtonDown (0)) {
  18.             MoveCamera (); //call this when you click the mouse
  19.         }
  20.     }
  21.  
  22.     void MoveCamera()
  23.     {
  24.         // Think ray of sunshine
  25.         // Creates a Ray from the Main camera to the mouse position (when clicked)
  26.         Ray cameraRay = Camera.main.ScreenPointToRay (Input.mousePosition);
  27.  
  28.         //creates new RayCastHit object to get info from raycast
  29.         RaycastHit cameraInfo;
  30.  
  31.         // check for collisions in the Ray
  32.         // out means the arg has no value but will be defined by the method
  33.  
  34.         // Basically Physics.Raycast starts from the camera to the mouse
  35.         // It uses info from the Raycase (camera info) to check for any collisions
  36.         // to make a new game object with the object the Ray collided with
  37.         // If the object has an Interactive Object tag, it calls the Interactable script
  38.         // (which is a component) and then calls the MoveToInteraction method, passing the player
  39.         // in as an argument
  40.         if (Physics.Raycast (cameraRay, out cameraInfo, Mathf.Infinity))
  41.         {
  42.             GameObject interactedObject = cameraInfo.collider.gameObject;
  43.             if (interactedObject.tag == "Interactable Object") {
  44.                 //work with the Interactable script on this object
  45.                 interactedObject.GetComponent<Interactable> ().MoveToInteraction (playerAgent);
  46.             }
  47.             else
  48.             {
  49.                 playerAgent.stoppingDistance = 0;
  50.                 playerAgent.destination = cameraInfo.point;
  51.             }
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement