I have made an interaction system for my game, relying primarily on two scripts, WorldInteraction and Interactable. When I start the game up and try to interact with an object or NPC, the game interacts with ALL the objects and NPCs in the scene. After that it works fine. I’ve been staring at this problem for a while and can’t find what’s wrong with my code.
WorldInteraction code:
UnityEngine.AI.NavMeshAgent playerAgent;
void Awake(){
playerAgent = GetComponent<UnityEngine.AI.NavMeshAgent>();
}
void Update () {
if (Input.GetMouseButtonDown (0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
GetInteraction ();
}
void GetInteraction(){
Ray interactionRay = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit interactionInfo;
if(Physics.Raycast(interactionRay, out interactionInfo, Mathf.Infinity)){
GameObject interactedObject = interactionInfo.collider.gameObject;
if (interactedObject.tag == "Interactable Object") {
interactedObject.GetComponent<Interactable> ().MoveToInteraction (playerAgent);
}
else {
playerAgent.stoppingDistance = 0f;
playerAgent.destination = interactionInfo.point;
}
}
}
Interactable code:
[HideInInspector]
public UnityEngine.AI.NavMeshAgent playerAgent;
private bool hasInteracted;
public virtual void MoveToInteraction(UnityEngine.AI.NavMeshAgent playerAgent){
hasInteracted = false;
this.playerAgent = playerAgent;
playerAgent.stoppingDistance = 2.0f;
playerAgent.destination = transform.position;
}
void Update(){
if(!hasInteracted && playerAgent != null && !playerAgent.pathPending){
float distance = Vector3.Distance (playerAgent.destination, transform.position);
if (playerAgent.remainingDistance < playerAgent.stoppingDistance) {
Interact ();
hasInteracted = true;
}
}
}
public virtual void Interact(){
Debug.Log ("Interacting with base class.");
}
Any help would be highly appreciated.