Navmesh Agent - The Y Next Position & Y Velocity does not update if there is no X velocity.
I'm using the Navmesh agent to make player pathfinding movement in a 2D game. I'm trying to implement 2 movement options, a) Mouse Click (works fine), b) WASD.
Currently, when using WASD, going straight up/down, so pressing only W or S, will not make the player move up or down on the Y axis.
After trying to Debug this I found out that the navAgent.nextPosition was not updating unless there was some X axis input. The same thing goes for the velocity (despite the Desired velocity to be correct).
It's my first time using Navmesh so I'm not too sure what could cause this, here is the debug result and some script snippet.
[SerializeField] private float playerSpeed = 3.4f;
[SerializeField] private ScriptObj_VectorValue startingPosition = default;
[SerializeField] private NavMeshAgent navAgent = default;
private Vector3 direction;
public bool mouseMovement;
private void Awake()
{
navAgent.updateRotation = false;
navAgent.updateUpAxis = false;
navAgent.updatePosition = false;
}
void Start()
{
transform.position = startingPosition.entryPos;
}
void FixedUpdate()
{
if (mouseMovement)
{
MouseMove();
}
else
{
WASDMove();
}
}
private void WASDMove()
{
direction.x = Input.GetAxisRaw("Horizontal");
direction.y = Input.GetAxisRaw("Vertical");
if (direction != Vector3.zero)
{
direction.x = transform.position.x + direction.normalized.x;
direction.y = transform.position.y + direction.normalized.y;
navAgent.destination = direction;
}
Vector2 nextPos = navAgent.nextPosition;
transform.position = Vector2.MoveTowards(transform.position, nextPos, playerSpeed * Time.deltaTime);
}
Your answer
Follow this Question
Related Questions
Creating prefab through Zenject Factory makes navMeshAgent act strange 0 Answers
how can I implement navmesh in this script? 0 Answers
NavMeshAgent dont find the right way on runtime build NavMesh 0 Answers
How to change speed for a cloned prefab instead of applying it to all instances of prefab? 1 Answer
ProBuilder-made object not generating Nav Mesh (Unity3D) 1 Answer