Hi all!
In this lesson I used a different stopping method to make the player stop at a certain distance and I want to ask if it is a wrong approach or may result in a bug latter on.
public class Mover : MonoBehaviour
{
Animator characterAnimator;
NavMeshAgent entityNavmeshAgent;
void Start()
{
characterAnimator = GetComponent<Animator>();
entityNavmeshAgent = GetComponent<NavMeshAgent>();
}
void Update()
{
UpdateAnimator();
}
public void MoveTo(Vector3 destination, float distance)
{
entityNavmeshAgent.destination = destination;
StopDistance(distance);
}
public void StopDistance(float distance)
{
entityNavmeshAgent.stoppingDistance = distance;
}
private void UpdateAnimator()
{
Vector3 velocity = entityNavmeshAgent.velocity;
Vector3 localVelocity = transform.InverseTransformDirection(velocity);
float speed = localVelocity.z;
GetComponent<Animator>().SetFloat("forwardSpeed", speed);
}
}
public class Fighter : MonoBehaviour
{
[SerializeField] float weaponRange = 2f;
Transform target;
private void Update()
{
if(target != null)
{
GetComponent<Mover>().MoveTo(target.position, weaponRange);
}
}
public void Attack(CombatTarget combatTarget)
{
if(combatTarget != null)
{
target = combatTarget.transform;
}
}
}
Let me know if my line of thinking is wrong in any way, I’m not a fully programmer so maybe I’m getting the wrong steps.
Thanks,
Carlos.