Search Unity

Player moving really slow?

Discussion in 'Scripting' started by Blackfire363, Oct 9, 2013.

  1. Blackfire363

    Blackfire363

    Joined:
    Oct 2, 2013
    Posts:
    10
    followed and check all my code about 20 times and still can not find the problem, I have my Main camera under my player Capsule
    with this code attached to the capsule

    Code (csharp):
    1.  
    2. var walkacceleration : float = 5;
    3. var cameraObject : GameObject;
    4. var maxwalkspeed : float = 20;
    5. @HideInInspector
    6. var horizontalmovement : Vector2;
    7.  
    8. function Update ()
    9. {
    10.     horizontalmovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
    11.  
    12.     if (horizontalmovement.magnitude > maxwalkspeed)
    13.     {
    14.         horizontalMovement = horizontalmovement.normalized;
    15.         horizontalmovement *= maxwalkspeed;    
    16.  
    17.     }
    18.     rigidbody.velocity.x = horizontalMovement.x;
    19.     rigidbody.velocity.z = horizontalMovement.y;
    20.    
    21.     transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLookScript).currentYrotation, 0);
    22.     rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkacceleration, 0, Input.GetAxis("Vertical") * walkacceleration);
    23. }
    24.  
    25.  
    These are the exact same as a person's tutorials and I still have no idea what is wrong, the player barely moves
     
  2. Marsupilami

    Marsupilami

    Joined:
    Sep 22, 2013
    Posts:
    21
    What's the mass of the rigidbody? Did you change it from the default of 1? Maybe try using AddForce as apposed to AddRelativeForce.
     
  3. Blackfire363

    Blackfire363

    Joined:
    Oct 2, 2013
    Posts:
    10
    I kept it at default of 1 and the guy said it was better to addrelative force :( I follow him exact, so i have no idea why mine barely moves
     
  4. Blackfire363

    Blackfire363

    Joined:
    Oct 2, 2013
    Posts:
    10
    Tried changing the mass, it speeds it up but will not hit "max" speed :( still stumped
     
  5. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,618
    try supplying the forcemode parameter to AddRelativeForce.

    experiment with the different types.
     
  6. TheBaconWalrus

    TheBaconWalrus

    Joined:
    Oct 8, 2013
    Posts:
    3
    You could try playing around with the top bit of your script, for example:

    Code (csharp):
    1.  
    2. var walkacceleration : float = 10;
    3. var cameraObject : GameObject;
    4. var maxwalkspeed : float = 45;
    5.  
    This should increase the speed of the character without having to put many physics options on your capsule.
     
  7. Blackfire363

    Blackfire363

    Joined:
    Oct 2, 2013
    Posts:
    10
    Aye I tired this ^ the player moves faster but still wont "accelerate" to get upto the maxwalkspeed
     
unityunity