Search Unity

Player moves wierdly in unity3d c#

Discussion in 'Scripting' started by Risna, Nov 28, 2014.

  1. Risna

    Risna

    Joined:
    Nov 28, 2014
    Posts:
    10
    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.

    1. // If user press W on the keyboard
    2. if(Input.GetKey(KeyCode.W))
    3. {
    4. // Move the player forward
    5. transform.position -=Vector3.forward * moveSpeed *Time.deltaTime;
    6. }
    7. // But if user press A on the keyboard
    8. elseif(Input.GetKey(KeyCode.A))
    9. {
    10. // Move the player to the right
    11. transform.position +=Vector3.right * moveSpeed *Time.deltaTime;
    12. }
    13. // But if user press S on the keyboard
    14. elseif(Input.GetKey(KeyCode.S))
    15. {
    16. // Move the player backward
    17. transform.position +=Vector3.forward * moveSpeed *Time.deltaTime;
    18. }
    19. // But if user press D on the keyboard
    20. elseif(Input.GetKey(KeyCode.D))
    21. {
    22. // Move the player to the left
    23. transform.position -=Vector3.right * moveSpeed *Time.deltaTime;
    24. }
    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.)

    1. int xVelocity =0, zVelocity =0;
    2. if(Input.GetKey(KeyCode.W))
    3. {
    4. xVelocity -=5;
    5. }
    6. elseif(Input.GetKey(KeyCode.A))
    7. {
    8. zVelocity -=5;
    9. }
    10. elseif(Input.GetKey(KeyCode.S))
    11. {
    12. xVelocity +=5;
    13. }
    14. elseif(Input.GetKey(KeyCode.D))
    15. {
    16. zVelocity +=5;
    17. }
    18. 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?
     
    Last edited: Nov 28, 2014
  2. fox4snce

    fox4snce

    Joined:
    Jan 25, 2014
    Posts:
    74
    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 :)

    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 :>
     
    Risna likes this.
  3. Risna

    Risna

    Joined:
    Nov 28, 2014
    Posts:
    10
    Thank you sir, it does help. And also I have check the freeze rotation on the rigidbody, it works perfectly and setlled. Thank you
     
  4. fox4snce

    fox4snce

    Joined:
    Jan 25, 2014
    Posts:
    74
    Glad to hear it! good luck :)
     
  5. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    Code (CSharp):
    1. code tags are your friends
     
    djfunkey likes this.