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

Begginer Jumping javascrpit problem.

Discussion in 'Scripting' started by SpiroDiscoBall, Aug 25, 2014.

  1. SpiroDiscoBall

    SpiroDiscoBall

    Joined:
    Aug 25, 2014
    Posts:
    4
    If i spam the jump button (w) my ball jumps the first time then when it lands it does not jump right away but it jumps randomly after a number of button presses.

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var rotationSpeed = 100;
    4. var jumpHeight = 8;
    5.  
    6. private var isFalling = false;
    7.  
    8. function Update ()
    9. {
    10.     //Handle ball rotation.
    11.     var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
    12.     rotation *= Time.deltaTime;
    13.     rigidbody.AddRelativeTorque (Vector3.back * rotation);
    14.    
    15.     if (Input.GetKeyDown(KeyCode.W) && isFalling == false)
    16.     {
    17.         rigidbody.velocity.y = jumpHeight;
    18.     }
    19.     isFalling = true;
    20. }
    21.  
    22. function OnCollisionStay ()
    23. {
    24.     isFalling = false;
    25. }

    I head this was a arithmetic behavior problem in java script so i tried in c# but its not working.
    Code (CSharp):
    1. public class BallControl : MonoBehaviour
    2. {
    3. public float rotationSpeed = 100;
    4. public float jumpHeight = 8;
    5.  
    6. private bool isFalling = false;
    7.  
    8.  
    9.  
    10. void Update ()
    11. {
    12. float rotation = Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime;
    13. rigidbody.AddRelativeTorque(Vector3.back * rotation);  
    14.  
    15. if(Input.GetKey("space") && isFalling == false)
    16. {
    17. isFalling = true;
    18. Vector3 jump = gameObject.rigidbody.velocity;
    19. jump.y = jumpHeight;
    20. gameObject.rigidbody.velocity = jump;
    21.  
    22. }
    23.  
    24.  
    25.  
    26. }
    27.  
    28. public void OnCollisionStay()
    29. {
    30. isFalling = false;
    31. }
    32. }

    I am a very beginner at programming in JavaScript, c# and this is the first game/project im making in unity. Help would be greatly appreciated.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you are calling the collision function incorrectly.... there isn't a unity engine function called "OnCollisionStay" that takes no arguments.

    http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionStay.html

    It always needs to have the argument of type collision.


    What you have done here is overload the unity engine function with your own definition (i.e. one without parameters) but the unity engine is never going to call that. What you want to do is override the unity engine function with your own code.
     
  3. SpiroDiscoBall

    SpiroDiscoBall

    Joined:
    Aug 25, 2014
    Posts:
    4
    The code was given to me by someone else. I have very little experience programming. I'm understanding the concept of what your saying but i have no idea how to put it into code.
     
  4. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    Don't worry about that. OnCollisionStay work also.

    Add OnCollisionExit(){isFalling=true;}
     
  5. SpiroDiscoBall

    SpiroDiscoBall

    Joined:
    Aug 25, 2014
    Posts:
    4
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var rotationSpeed = 100;
    4. var jumpHeight = 8;
    5.  
    6. private var isFalling = false;
    7.  
    8. function Update ()
    9. {
    10.     //Handle ball rotation.
    11.     var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
    12.     rotation *= Time.deltaTime;
    13.     rigidbody.AddRelativeTorque (Vector3.back * rotation);
    14.    
    15.     if (Input.GetKeyDown(KeyCode.W) && isFalling == false)
    16.     {
    17.         rigidbody.velocity.y = jumpHeight;
    18.     }
    19.     isFalling = true;
    20. }
    21.  
    22. function OnCollisionStay ()
    23. {
    24.     isFalling = false;
    25. }
    26.  
    27. function OnCollisionExit()
    28. {
    29. isFalling=true;
    30. }
    Like this?
    It still gives me the same problem.
     
  6. SpiroDiscoBall

    SpiroDiscoBall

    Joined:
    Aug 25, 2014
    Posts:
    4
    Bump. Still in need of help.