I have a function that gets called with Right mouse Click, it suppose to get called once per unit that has this script, but for me its getting called approximately 100x per script.
Thanks in advance
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class ClickToMove : MonoBehaviour
{
private Animator mAnimator;
private NavMeshAgent mNavMeshAgent ;
private bool mRunning = false;
float count = 1;
void Start()
{
mAnimator = GetComponent<Animator>();
mNavMeshAgent = GetComponent<NavMeshAgent>();
}
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Input.GetMouseButtonDown(1))
{
CalculateStoppingDistance();
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
if (mNavMeshAgent.tag == "Selected")
{
mNavMeshAgent.destination = hit.point;
}
}
}
if (mNavMeshAgent.remainingDistance <= mNavMeshAgent.stoppingDistance)
{
mRunning = false;
}
else
{
mRunning = true;
}
mAnimator.SetBool("Running", mRunning);
}
void CalculateStoppingDistance()
{
foreach (Unit myUnit in SelectionManager.SelectedUnitList)
{
if (mNavMeshAgent.tag == "Selected")
{
count = SelectionManager.SelectedUnitList.Count;
mNavMeshAgent.stoppingDistance = count ;
Debug.Log("count = " + count);
Debug.Log("Stopping Distance = " + mNavMeshAgent.stoppingDistance);
}
}
}
}