Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5.  
  6. // This class will be used as a base for all our interactable objects
  7. public class Interactable : MonoBehaviour {
  8.    
  9.     [HideInInspector]
  10.     public NavMeshAgent playerAgent; //can't be private because this is passed around between objects
  11.  
  12.     private bool hasInteracted;
  13.     // move to object before we interact with it
  14.     // 'virtual' allows us to override this function
  15.     public virtual void MoveToInteraction(NavMeshAgent playerAgent) //in this case this is the player
  16.     {                                                               //passed in from WorldInteraction
  17.         hasInteracted = false;
  18.         this.playerAgent = playerAgent; //sets this class' playerAgent to the Player
  19.         playerAgent.stoppingDistance = 4f;
  20.         playerAgent.destination = this.transform.position;
  21.  
  22.     }
  23.  
  24.     void Update()
  25.     {
  26.         if(!hasInteracted && playerAgent != null && !playerAgent.pathPending) //make sure player exists and isn't pathfinding
  27.         {
  28.             if(playerAgent.remainingDistance <= playerAgent.stoppingDistance)
  29.             {
  30.                 Interact ();
  31.                 hasInteracted = true;
  32.             }
  33.         }
  34.     }
  35.  
  36.     // this is the base class to be overwritten by interable objects
  37.     public virtual void Interact()
  38.     {
  39.         Debug.Log("Interacting with base class");
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement