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

Button doesnt respond?!

Discussion in 'Scripting' started by Lizzard4000, Aug 30, 2011.

  1. Lizzard4000

    Lizzard4000

    Joined:
    Mar 3, 2010
    Posts:
    101
    hi,

    im working on a simple jumpNrun testscene.
    weird thing is, jumping (Arrow UP) works perfectly in the editor and webplayer but not on the standalone exe.
    works/ doesnt work with joystick too.

    Webplayer works:
    deletedurl

    standalone exe doesnt work:
    deletedurl

    my code:

    Code (csharp):
    1.  
    2. var BUTTON_LEFT = false;
    3. var BUTTON_RIGHT= false;
    4. var BUTTON_JUMP= false;
    5. var BUTTON_JUMP_DOWN= false;
    6.  
    7. private var jumping=450.0;
    8. var collieded=0;
    9. function Update () {
    10.  
    11. if (Input.GetKey(KeyCode.LeftArrow) || Input.GetAxis("Horizontal") < -0.5 ) {
    12. BUTTON_LEFT = true;
    13. }else{
    14. BUTTON_LEFT = false;
    15. }
    16.  
    17. if (Input.GetKey(KeyCode.RightArrow) || Input.GetAxis("Horizontal") > 0.5 ) {
    18. BUTTON_RIGHT= true;
    19. }else{
    20. BUTTON_RIGHT = false;
    21. }
    22.  
    23. if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.Joystick1Button0)) {
    24. BUTTON_JUMP= true;
    25. }else{
    26. BUTTON_JUMP = false;
    27. }
    28.  
    29. if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Joystick1Button0)) {
    30. BUTTON_JUMP_DOWN= true;
    31. }else{
    32. BUTTON_JUMP_DOWN = false;
    33. }
    34. //Buttons
    35. ///////////////////////////////////////////////////////////////  
    36.  
    37. }
    38.  
    39.  
    40.  
    41. function FixedUpdate () {
    42.    rigidbody.AddForce (0, -35, 0);
    43.    rigidbody.angularDrag = 100;
    44.    
    45.    if (BUTTON_LEFT) {
    46.     rigidbody.angularDrag = 0;
    47.    rigidbody.AddTorque(0, 0,30);
    48.    rigidbody.AddForce (-7, 0, 0);
    49.    }
    50.       if (BUTTON_RIGHT) {
    51.        rigidbody.angularDrag = 0;
    52.    rigidbody.AddTorque(0, 0,-30);
    53.      rigidbody.AddForce (7, 0, 0);
    54.    }
    55.    
    56.    
    57.    if (collieded==0) {
    58.       if (BUTTON_LEFT) {
    59.     rigidbody.angularDrag = 0;
    60.    rigidbody.AddTorque(0, 0,30);
    61.    rigidbody.AddForce (-30, 0, 0);
    62.    }
    63.       if (BUTTON_RIGHT) {
    64.        rigidbody.angularDrag = 0;
    65.    rigidbody.AddTorque(0, 0,-30);
    66.      rigidbody.AddForce (30, 0, 0);
    67.    }
    68.    }
    69.    
    70.      if (BUTTON_JUMP_DOWN) {
    71.         jumping = 450;
    72.      }
    73.    
    74.     if (BUTTON_JUMP) {
    75.     jumping = jumping-40;
    76.     if (jumping <= 0) {
    77.     jumping = 0;
    78.     }
    79.     rigidbody.AddForce (0,jumping, 0);
    80.     }
    81.    
    82. }
    83.  
    84.  
    85. function OnCollisionStay() {
    86. collieded=1;
    87. }
    88.  
    89. function OnCollisionExit() {
    90. collieded=0;
    91. }
    92.  
    93.  
     
    Last edited: Aug 30, 2011
  2. UnknownProfile

    UnknownProfile

    Joined:
    Jan 17, 2009
    Posts:
    2,311
    In the web player, you can jump even if you aren't touching the ground, just a possible bug. I can't test out the executable because I'm on a Mac.
     
  3. Lizzard4000

    Lizzard4000

    Joined:
    Mar 3, 2010
    Posts:
    101
    yeah endless jumping is OK
     
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    OK, so lets get your code down to something that looks easier to read. Lets start by getting rid of all your "button down" stuff. None of that is necessary. We can do all of this in the FixedUpdate since it all deals with forces.

    Code (csharp):
    1.  
    2. var jumpForce=450.0;
    3.  
    4. funciton Start(){
    5.     if(!rigidbody)
    6.         gameObject.AddComponent("Rigidbody");
    7.     rigidbody.angularDrag = 100;
    8. }
    9.  
    10. function FixedUpdate () {
    11.     // add gravity
    12.     rigidbody.AddForce (0, -35, 0);
    13.    
    14.     // get controls
    15.     var isGrounded = Physics.Raycast(transform.position, -Vector3.up, 0.6);
    16.     var jump = Mathf.Clamp01(Input.GetAxisRaw("Vertical"));
    17.    
    18.     // setup bare forces
    19.     var torque=Vector3(0,0,Input.GetAxis("Horizontal"));
    20.     var force=Vector3(Input.GetAxis("Horizontal"),0,0);
    21.    
    22.     // check for forces according to if we are on the ground or not.
    23.     if(isGrounded){
    24.         torque*=30;
    25.         force*=30;
    26.         force.y=jump * jumpForce;
    27.     } else {
    28.         torque*=30;
    29.         force*=7;
    30.     }
    31.    
    32.     // apply forces if needed.
    33.     if(torque.magnitude > 0.0) rigidbody.AddTorque(torque);
    34.     if(force.magnitude > 0.0) rigidbody.AddForce (force);
    35. }
    36.  
    You will see I am doing a Physics.Raycast check from the object, downward. If I see something, we are on the ground, if not, we are not. So this gets rid of the collision stuff you had before.

    Also, I had corrected the logic so that if we are not on the ground, we are not jumping.

    I dumped all the arrow stuff, and am solely using Input.GetAxis and Input.GetAxisRaw to get if we pressed buttons. This is because the arrows and WASD keys are all part of GetAxis and GetAxisRaw. (GetAxisRaw is -1, 0 or 1 thus eliminating the >0.5 stuff you had.)

    next, I am using Math to calculate the force and torque. I know that if you are not on the ground, your force is less, so I added the multipliers in the right places to get your actual force. I also added the jump factor, but only in the section where we are grounded.

    I hope this helps you out.
     
  5. Lizzard4000

    Lizzard4000

    Joined:
    Mar 3, 2010
    Posts:
    101
    thx for answers.

    solution:
    if (BUTTON_JUMP_DOWN) {
    jumping = 450;
    }
    should be in Update()