Player moves wierdly in unity3d c#

I am not sure what the proper question is for my problem, so would like to explain how my character is moving with different code, and when I unattach rigidbody, and hope you can help me figure it out.

Here is my case:

I have walls that will stop the player as it colides with the wall, and of course I have rigidbody and character controller attached to the player.

When I run the game using this code below, the player moves and collides with the walls, but it moves too fast, and when I press a key continuously the player goes past the wall and is floating in space.

  • // If user press W on the keyboard
  • if(Input.GetKey(KeyCode.W))
  • {
  • // Move the player forward
  • transform.position -=Vector3.forward * moveSpeed *Time.deltaTime;
  • }
  • // But if user press A on the keyboard
  • elseif(Input.GetKey(KeyCode.A))
  • {
  • // Move the player to the right
  • transform.position +=Vector3.right * moveSpeed *Time.deltaTime;
  • }
  • // But if user press S on the keyboard
  • elseif(Input.GetKey(KeyCode.S))
  • {
  • // Move the player backward
  • transform.position +=Vector3.forward * moveSpeed *Time.deltaTime;
  • }
  • // But if user press D on the keyboard
  • elseif(Input.GetKey(KeyCode.D))
  • {
  • // Move the player to the left
  • transform.position -=Vector3.right * moveSpeed *Time.deltaTime;
  • }

When I change the code to the below code (Let the Physic Engine do the work) the player does not move through the wall anymore when the key is pressed continuously, but the rotation of the player become weird (e.g: The player is in standing position, but when I press W, A, S or D, the rotation of the player is not standing position anymore.)

  • int xVelocity =0, zVelocity =0;
  • if(Input.GetKey(KeyCode.W))
  • {
  • xVelocity -=5;
  • }
  • elseif(Input.GetKey(KeyCode.A))
  • {
  • zVelocity -=5;
  • }
  • elseif(Input.GetKey(KeyCode.S))
  • {
  • xVelocity +=5;
  • }
  • elseif(Input.GetKey(KeyCode.D))
  • {
  • zVelocity +=5;
  • }
  • rigidbody.velocity =newVector3(xVelocity,0, zVelocity);

When I unattached the rigidbody from the player and move back to the first code, the player does not move weirdly, but it does not check for the collision when the player collides with the walls.

Can someone help me with this?

My best guess is that when you use transform.position you are moving too far per step/frame.

It’s like a bunch of dots… * * * * * *
Over and over again… first you appear at position one, then you skip over the middle and land at position 2.
If there’s a wall between the two positions | you will still move right through it.

On the other hand, when you use the built-in physics, it’s taking into account this kind of movement and so when you move each frame, it checks to see if you’ve moved through a wall and if so, backs you up and says “nope, nuh uh.”

Also, you are adding velocity with += … if my velocity is 1, I move like this

  • → *
    but if I add 5 to it… I move like this…
    *-> *
    Then when I add 5 velocity again… suddenly I’m moving like this
    *-> *
    PER FRAME :slight_smile:

That’s why the movement is weird in your second case.

The solution here should be to either set the velocity to 5 or -5 OR you can set a boundary for the velocities…
if xVelocity is greater than 5 then xVelocity equals 5

Hope that helps :>

1 Like

Thank you sir, it does help. And also I have check the freeze rotation on the rigidbody, it works perfectly and setlled. Thank you

Glad to hear it! good luck :slight_smile:

code tags are your friends
1 Like