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

Inputs not working. (SOLVED)

Discussion in 'Scripting' started by sammyandsam, Aug 15, 2015.

  1. sammyandsam

    sammyandsam

    Joined:
    Aug 15, 2015
    Posts:
    11
    I'm new to Unity and watched a Tutorial on a basic game.
    I have space for jump, but when I press space, it sometimes jump. It doesn't always jump.
    Here is my code for the controls:
    http://pastebin.com/mYX1EPYh
    Sorry if this is the wrong thread.
     
  2. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    The isFalling should be set to true inside the if statemen.

    Code (CSharp):
    1.  if (Input.GetKeyDown(KeyCode.Space) && isFalling == false)
    2.         {
    3.     isFalling = true;
    4.                 GetComponent.<Rigidbody>().velocity.y = jumpHeight;
    5.         }
    6.            
    Not outside it.
     
  3. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    Its probably not the input that is being missed, but since (as korno has said) you had the isfalling being set to true outside the if, it was being set every frame. Meanwhile, OnCollisionStay, where you are setting isfalling to false, is only ran every fixedupdate, which depending on your settings could mean that some updates the isfalling was never set back to false since OnCollisionStay was not ran.
     
  4. sammyandsam

    sammyandsam

    Joined:
    Aug 15, 2015
    Posts:
    11
    Now when I jump, I can jump again in the air.
     
  5. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    I dont know javascript, so hopefully this is coded right

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

    Would this work? Not tested.
     
  6. sammyandsam

    sammyandsam

    Joined:
    Aug 15, 2015
    Posts:
    11
    No, that didn't work. Its doing the same thing from the original post.
     
  7. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    hehe, aaaa yes, all I did was re-add the problem of fixedupdate.

    have a look at how its being done here
    http://wiki.unity3d.com/index.php?title=RigidbodyFPSWalker

    Another way might be like this
    Code (JavaScript):
    1. #pragma strict
    2. var rotationSpeed = 100;
    3. var jumpHeight = 8;
    4. private var isFalling = false;
    5. private var canJump = false;
    6.  
    7. function Update()
    8. {
    9.     if(Input.GetKeyDown(KeyCode.Space) && isFalling == false)
    10.     {
    11.         canJump = true;
    12.     }
    13. }
    14.  
    15. function FixedUpdate()
    16. {
    17.     var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
    18.     rotation *= Time.deltaTime;
    19.     GetComponent.<Rigidbody>().AddRelativeTorque (Vector3.back * rotation);
    20.  
    21.     if(canJump == true)
    22.     {
    23.         GetComponent.<Rigidbody>().velocity.y = jumpHeight;
    24.         canJump = false;
    25.     }
    26.     isFalling = true;
    27. }
    28.  
    29. function OnCollisionStay()
    30. {
    31.     isFalling = false;
    32. }
     
    Last edited: Aug 15, 2015
  8. sammyandsam

    sammyandsam

    Joined:
    Aug 15, 2015
    Posts:
    11
    I get an error:
    Assets/Scripts/BallControl.js(24,21): BCE0034: Expressions in statements must only be executed for their side-effects.
     
  9. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    oops, do canJump = false, not canJump == false.
    Sorry, writing this without a IDE
    Ill edit the code.
     
  10. sammyandsam

    sammyandsam

    Joined:
    Aug 15, 2015
    Posts:
    11
    That fixed it. But, how can I make it so I can move while I jump?
     
  11. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    what is your current movement script?
    If you are to use the RigidbodyFPSController that I linked above, to move while in the air you would move the movement part of the script out of the if(grounded)
     
  12. sammyandsam

    sammyandsam

    Joined:
    Aug 15, 2015
    Posts:
    11
    Im using
    #pragma strict
    var rotationSpeed = 100;
    var jumpHeight = 8;
    private var isFalling = false;
    private var canJump = false;

    function Update()
    {
    if(Input.GetKeyDown(KeyCode.Space) && isFalling == false)
    {
    canJump = true;
    }
    }

    function FixedUpdate()
    {
    var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
    rotation *= Time.deltaTime;
    GetComponent.<Rigidbody>().AddRelativeTorque (Vector3.back * rotation);

    if(canJump == true)
    {
    GetComponent.<Rigidbody>().velocity.y = jumpHeight;
    canJump = false;
    }
    isFalling = true;
    }

    function OnCollisionStay()
    {
    isFalling = false;
    }
    I would want to move right or left while in the air. When I jump, I just move the way I was moving before I jumped.
     
  13. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    I dont see any movement code in the script you posted, only jump and rotation.
     
  14. sammyandsam

    sammyandsam

    Joined:
    Aug 15, 2015
    Posts:
    11
    Nevermind then. I don't need it that much.