I followed the tutorial carefully this time, it’s probably something obvious I am overlooking in the scripts.
He goes in circles after I click somewhere as well.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Mover : MonoBehaviour
{
[SerializeField] Transform target;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
MoveToCursor();
}
UpdateAnimator();
}
private void MoveToCursor()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
bool hasHit = Physics.Raycast(ray, out hit);
if (hasHit)
{
GetComponent<UnityEngine.AI.NavMeshAgent>().destination = hit.point;
}
}
private void UpdateAnimator()
{
Vector3 velocity = GetComponent<UnityEngine.AI.NavMeshAgent>().velocity;
Vector3 localVelocity = transform.InverseTransformDirection(velocity);
float speed = localVelocity.z;
GetComponent<Animator>().SetFloat("forwardSpeed", speed);
}
}