After colliding, Player moves without input

After the player collides into an object, the player is unable to move through the object due to the collider (this is as expected). When the movement input (WASD or arrow keys) are NOT being pressed, the player begins moving in the opposite direction of what it collided with (sometimes fast, sometimes slow).

When it occurs, trying to move in the opposite direction it is traveling results in a “tug-of-war” between the human input and whatever is going on. Any other input (including colliding with another object) results in whatever is going on to stop.

I am assuming that something is causing the player to think it is being pushed on and a programmatic reset of this would resolve it. When I put GetComponent().velocity = new Vector3(0, 0, 0) inside the OnCollisionEnter method of the player, this behavior continued.

Why is this happening and how do I resolve it?

Movement is calculated using the following:
float
X = Input.GetAxis(“Horizontal”) * Time.deltaTime * MoveSpeed,
Y = 0.0f,
Z = Input.GetAxis(“Vertical”) * Time.deltaTime * MoveSpeed;
transform.Translate(X, Y, Z);

To resolve a different issue of the player passing through the collider, I have update the Default Max Depenetration Velocity (Edit > Project Settings > Physics) from 10 to 1000.

I am using Unity Version: 2020.3.22f1.1228 Personal

Hi,

Good job on analysing the situation. The reason for this behaviour is very likely that there is no friction (yet). When the player bumps into another collider, it bounces off.

One solution could be to set the velocity to (0f, 0f, 0f) in OnCollisionExit. The forces get applied after the collision happened. If you set the velocity to (0f, 0f, 0f) in OnCollisionEnter, the physics simulation could still modify the player’s velocity afterwards.

Another potential solution could be to create a Physics Material in the Assets folder, increase its friction and assign it to the ground. You’ll probably have to tweak the values a bit to achieve a good result.

Did this help?


See also:

I moved the velocity reset to the method OnCollisionExit. The first time I ran the code, the player ran right through the wall. After verifying the physics had not changed; I could not repeat it going through the wall.

I was also not able to reproduce the wandering player issue. So, for now I would say this has resolved the issue. Once the friction has been added (as hinted above) I will comment this out and see what happens.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms