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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Joystick script (need help!)

Discussion in 'Scripting' started by AAcat, Dec 3, 2015.

  1. AAcat

    AAcat

    Joined:
    Apr 11, 2015
    Posts:
    19
    I'm not too familiar with joystick controls but i attempted to make a jump button code in JavaScript
    Code (JavaScript):
    1. private var isFalling = false;
    2.  
    3. function OnCollisionEnter ()
    4. {
    5.     isFalling = false;
    6. }
    7. function OnCollisionExit ()
    8. {
    9.     isFalling = true;
    10. }
    11. function Update ()
    12. {
    13.     var djown  = Input.GetButtonDown ("Jump");
    14.     var rotation : float = Input.GetAxis("Horizontal") * rotationSpeed;
    15.     rotation *= Time.deltaTime;
    16.     GetComponent.<Rigidbody>().AddRelativeTorque(Vector3.down * rotation);
    17.    
    18.     if (Input.GetKeyDown (KeyCode.Space) && isFalling == false)
    19.     {
    20.         GetComponent.<Rigidbody>().velocity.y = jumpHight;
    21.     }
    22.     if ("djown" && isFalling == false)
    23.     {
    24.         Debug.Log("test");
    25.     }
    26. }
    evertything works except for my last if statement. It's not suppose to print test unless i press the button (which I'm not) but it continues to spam the console anyways with the word "test". The word "test:" is only suppose to appear if i do press the button and isFalling == false. both isFalling and joystick button are not true, any ideas?
     
  2. Fajlworks

    Fajlworks

    Joined:
    Sep 8, 2014
    Posts:
    344
    Code (CSharp):
    1.  if ("djown" && isFalling == false)   <----- "djown"
    2. {
    3.       Debug.Log("test");
    4. }
    I think you meant to write:
    Code (CSharp):
    1. if (djown && !isFalling)
    2. {
    3.     // rest of code
    4. }
    You were using string "djown" instead your bool variable in that check. Try to see if it works now.
     
    AAcat likes this.
  3. AAcat

    AAcat

    Joined:
    Apr 11, 2015
    Posts:
    19
    Thanks for the help but i figured out my problem. Since my character starts off in mid air the " OnCollisionEnter" never happened nor the "OnCollisionExit" so it is set to false automatically without being able to change to true. So it has to keep spam it, and since i didn't use the variable right by adding quotes it just ignored it.