Reputation: 21
NavMeshAgent character jitters while we both move same direction. And Same as croshair also Jitters.
public class AiLocomotion : MonoBehaviour
{
private PlayerController player;
public float maxTime = 1.0f;
public float maxDistance = 1.0f;
NavMeshAgent agent;
Animator anim;
float timer = 0.0f;
void Start()
{
agent = GetComponent<NavMeshAgent>();
anim = GetComponent<Animator>();
player = FindAnyObjectByType<PlayerController>();
}
private void FixedUpdate()
{
timer -= Time.deltaTime;
if (timer < 0.0f)
{
float sqrDistance = (player.transform.position - agent.destination).sqrMagnitude;
if (sqrDistance > maxDistance * maxDistance)
{
agent.destination = player.transform.position;
}
timer = maxTime;
}
anim.SetFloat("speed", agent.velocity.magnitude);
}
}
public class Cross : MonoBehaviour
{
public Transform target;
public float offset;
public float distance;
void Start()
{
}
void FixedUpdate()
{
if (Physics.Raycast(target.position, target.forward, out RaycastHit hit, distance))
transform.position = Camera.main.WorldToScreenPoint(hit.point);
else
transform.position = Camera.main.WorldToScreenPoint(target.position + (target.forward * offset));
}
}
I am traying to create an enemy character that follows player but when we move same direction enemy jitters. Also A crosshair for my character Jitters. I made researches but could not find a proper solution. I found this solution : Video It was okay but I could not use timer with this solution, it is not working with float timer.
Could anybody help ?
Upvotes: 1
Views: 253
Reputation: 36
My suggestion is that the camera is jittering, a common issue. Put your camera controller logic in LateUpdate() or use Script Execution Order to delay it even further. It appears to me that the Agent moves smoothly when the camera is still and that also the stationary objects jitter.
You can also stop the camera movement and see if anything changes.
If this is not helping then maybe you have to post more of your code.
Upvotes: 1