Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Ball Movement speed issue [JS]

Discussion in 'Scripting' started by Quist, Apr 25, 2015.

  1. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    I´m making a ball game in 3D in JS but i have the problem that whenever my player goes down a hill etc. it gains
    A LOT Speed which it shouldn´t.

    How do i limit its max speed?

    My script:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var jumpHeight = 0.02;
    4. static var jumpCount = 0;
    5. var canJump = true;
    6.  
    7. var horizontalSpeed : float;
    8. var verticalSpeed : float;
    9. var maxSpeed : float;
    10.  
    11.  
    12. function Update ()
    13. {
    14.         if (Input.GetKey (KeyCode.W) && jumpCount == 0)
    15.         {
    16.             GetComponent.<Rigidbody>().AddForce (0, 0, 12 * verticalSpeed);
    17.         }
    18.         if (Input.GetKey (KeyCode.S))
    19.         {
    20.             GetComponent.<Rigidbody>().AddForce (0, 0, -8 * verticalSpeed);
    21.         }
    22.         if (Input.GetKey (KeyCode.A))
    23.         {
    24.             GetComponent.<Rigidbody>().AddForce (-11, 0, 0 * horizontalSpeed);
    25.         }
    26.         if (Input.GetKey (KeyCode.D))
    27.         {
    28.             GetComponent.<Rigidbody>().AddForce (11, 0, 0 * horizontalSpeed);
    29.         }
    30.         if (Input.GetKey (KeyCode.Space) && jumpCount == 0)
    31.         {
    32.             GetComponent.<Rigidbody>().AddForce(Vector3.up * jumpHeight, ForceMode.Impulse); jumpCount = 1;
    33.         }  
    34.        
    35. }
    36.  
    37. function OnCollisionEnter (hit : Collision)
    38. {
    39.   if(hit.gameObject.tag == "Floor")
    40.   {
    41.   jumpCount = 0;
    42.   }
    43. }
    44.  
     
  2. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    No one :(?
     
  3. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    set a terminal velocity on the ball so it can never go faster than a certain speed. could do this by clamping the velocity