Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

ETeeskiTutorials FPS Problems [ FPS 1.4 ] Setting a Max Walk Speed (flying glitch)

Discussion in 'Scripting' started by Galen Altaiir, Nov 1, 2013.

  1. Galen Altaiir

    Galen Altaiir

    Joined:
    Nov 1, 2013
    Posts:
    2
    I was copying code from FPS1.4 Setting a Max Walk Speed and in testing section I found out that player still flies after updating "PlayerMovement" script. Anybody know where I made the mistake?
    Link to youtube video http://www.youtube.com/watch?v=GddQmaFLZ00
    Code:

    Code (csharp):
    1. #pragma strict
    2.  
    3. function Start () {
    4.  
    5. }
    6.  
    7. var walkAcceleration : float = 5;
    8. var cameraObject : GameObject ;
    9. var maxWalkSpeed : float = 20;
    10. @HideInInspector
    11. var horizontalMovement : Vector2;
    12.  
    13.  
    14. function Update ()
    15. {
    16.     horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
    17.     if (horizontalMovement.magnitude > maxWalkSpeed)
    18.     {
    19.         horizontalMovement = horizontalMovement.normalized;
    20.         horizontalMovement *= maxWalkSpeed;
    21.        
    22.     }
    23.     rigidbody.velocity.x = horizontalMovement.x;
    24.     rigidbody.velocity.y = horizontalMovement.y;
    25.    
    26.     transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLookScript).currentYRotation, 0);
    27.     rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration, 0, Input.GetAxis("Vertical") * walkAcceleration);
    28. }