0
\$\begingroup\$

This is a weird one. I'm using a 2d adaptation of the NavMesh (found here: https://github.com/h8man/NavMeshPlus) to get obstacle avoidance for my AI in my 2d game.

I also want to use it for my player controller, so that I can get collisions with walls for 'free' without having to use a box or circle collider and deal with any clipping issues. So currently my player component has a Rigidbody2d set to Kinematic, a circle collider (to register collisions with bullets etc.), and a Nav Mesh Agent component. In the Start method of my script, I was told to add these lines:

agent.updateRotation = false;
agent.updateUpAxis = false;

I used to use this method of moving the player in the FixedUpdate loop (which also has vertical-movement jittery issues when paired with the Nav Mesh Agent):

transform.Translate(new Vector3(moveVal.x, moveVal.y, 0) * moveSpeed * Time.fixedDeltaTime, Space.World);

However I read that I'd want to set the agent's velocity to make it better suited to being avoided by enemy AI, if required. So I tried...

agent.velocity = new Vector3(moveVal.x, moveVal.y, 0) * moveSpeed;

Ok, this works... but it only works in the x direction. If I hit W or S on keyboard to move vertically, it does nothing. However, if I hold both vertical AND horizontal move keys down, it works! ...bizarre.

I did try this with joystick movement and it also worked fine, which is also bizarre. I logged out moveVal.x and moveVal.y for both control methods; the keyboard values were always either 1, 0, or -1, whereas the gamepad/joystick values were floats anywhere between -1 and 1. (I'm using new input system with an action type of Value and control type Vector2)

And yes, I did try commenting out the agent.updateUpAxis = false line since it did look suspicious to me too, but that didn't change anything unfortunately.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

The issue you're experiencing might be related to the way you're updating the agent's velocity. When using keyboard input, the agent's velocity might not be updated correctly due to the combined input values being too large or too small. Here's a suggestion to improve the player movement:

Normalize your input vector to ensure the player's speed is consistent in all directions:

Vector2 moveValNormalized = moveVal.normalized;

Update the agent's velocity using the normalized input values:

agent.velocity = new Vector3(moveValNormalized.x, moveValNormalized.y, 0) * moveSpeed;

In your FixedUpdate loop, make sure you're calling agent.nextPosition to update the agent's position:

private void FixedUpdate()
{
    agent.nextPosition = transform.position;
}

With these changes, your player should move smoothly in all directions using both keyboard and joystick inputs. If you still experience issues with the vertical movement, you may want to debug the input values to ensure they're being read correctly. Additionally, make sure there are no other scripts or components interfering with the player's movement.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .