Tried to post this to Unity Answers but my post seems to be stuck into moderation queue, trying here.
I’m using the Navmesh agent to make player pathfinding movement in a 2D game (XY). I’m trying to implement 2 movement options, a) Mouse Click (which works fine), b) WASD (where the problem lies).
Currently, the player will not move up or down when attempting to go straight up or straight down (pressing only W or S).
After trying to Debug this I found out that the Y axis of the navAgent.nextPosition was not updating unless there was some X axis input. The same thing goes for the navAgent.velocity (despite the navAgent.desiredVelocity 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. I’m trying to use with my WASD movement so I don’t have to add a bunch of colliders on my environment (currently using navmesh obstacles which I carve).
Here’s a image of the debug, a snippet of the script and an image of my navAgent:
using UnityEngine;
using UnityEngine.AI;
public class Script_Player_Controls : MonoBehaviour
{
[Header("Mouse Player Movement")]
[SerializeField] private Camera myCamera = default;
[SerializeField] private ScriptObj_VectorValue startingPosition = default;
[SerializeField] private ScriptObj_UIBools UIBoolManager = default;
[SerializeField] private float playerSpeed = 3.4f;
private Vector2 animationTargetPos;
[Header("WASD Player Movement")]
private Vector3 direction;
[Header("Pathfinding")]
[SerializeField] private NavMeshAgent navAgent = default;
[SerializeField] private int rayDistance = 100;
[SerializeField] private float timeBeforeAnimSwitch = 0.5f;
private void Awake()
{
navAgent.updateRotation = false;
navAgent.updateUpAxis = false;
navAgent.updatePosition = false;
playerSpeed = navAgent.speed;
}
void Start()
{
transform.position = startingPosition.entryPos;
}
void FixedUpdate()
{
MouseMove();
WASDMove();
Vector2 nextPos = navAgent.nextPosition;
transform.position = Vector2.MoveTowards(transform.position, nextPos, playerSpeed * Time.deltaTime);
}
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;
}
Debug.Log("NextPos: " + navAgent.nextPosition + ". Destination: " + navAgent.destination + ". Velocity: " + navAgent.velocity + ". Desired Velocity: " + navAgent.desiredVelocity); //Don't worry this atrocity will get removed.
}
private void MouseMove()
{
MoveAnimation(); //Handles Animations, will be reworked
MoveRestrictions(); //Handles movement restrictions, will be reworked
if (Input.GetMouseButton(0) && currentState == PlayerState.walk)
{
Ray ray = myCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, rayDistance))
{
navAgent.destination = new Vector2(hit.point.x, hit.point.y);
}
animationTargetPos = myCamera.ScreenToViewportPoint(Input.mousePosition);
}
}