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

jump script error

Discussion in 'Scripting' started by Dinny1001, Jan 4, 2015.

  1. Dinny1001

    Dinny1001

    Joined:
    Jan 4, 2015
    Posts:
    11
    Hi,I'm new to scripting and Unity and everything. I'm watching a step by step tutorial on making a ball rolling game on Youtube. Even though i wrote the script exactly as the Youtuber did, every time i press w(which i scripted to make the ball jump) my ball does nothing,it can only roll to the left and right. Can someone please tell me what I'm doing wrong? I'm using javascript if that helps.


    #pragma strict

    var rotationSpeed = 100;
    var jumpHeight = 8;

    private var isFalling = false;

    function Update ()
    {
    var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
    rotation *= Time.deltaTime;
    rigidbody.AddRelativeTorque (Vector3.back * rotation);

    if (Input.GetKeyDown(KeyCode.W) && isFalling == false)
    {
    rigidbody.velocity.y = jumpHeight;
    }
    isFalling = true;
    }

    function OnCollsionStay ()
    {
    isFalling = false;
    }
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Check this line and see if it might need to go in the bracketed section above it:
    isFalling = true;

    Also, read the section about using code tags in the stickies.
     
  3. Dinny1001

    Dinny1001

    Joined:
    Jan 4, 2015
    Posts:
    11
    Thanks for the help but when i put isFalling = true; inside the brackets above,I can only jump once and once only.I want to be able to jump multiple times.Also,where can i find the "code tag by the stickies."?
     
    Last edited: Jan 5, 2015
  4. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    just tried it, you misspelt the collision function name.

    yeah ive had that before when unity just ignores it if its not spelt correctly :(

    Was this originally.
    Code (JavaScript):
    1. function OnCollsionStay ()
    2. {
    3. isFalling = false;
    4. }
    should read
    Code (JavaScript):
    1. function OnCollisionStay ()
    2. {
    3. isFalling = false;
    4. }
    and like fire7side said, put the isFalling inside the function block.
    Code (JavaScript):
    1. if (Input.GetKeyDown(KeyCode.W) && isFalling == false)
    2. {
    3. rigidbody.velocity.y = jumpHeight;
    4. isFalling = true;
    5. }
    should work just dandy now :)
     
    Last edited: Jan 5, 2015
  5. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819

    Nice find OboShape. Unity ignores it because it's legitimate. It's just another function with a different name. Really hard to find. I looked for spelling errors and didn't see it.
     
    OboShape likes this.
  6. Dinny1001

    Dinny1001

    Joined:
    Jan 4, 2015
    Posts:
    11
    Thank you so much for the help.my game works now but i have isFalling = true; outside yet it still works but thanks anyways! :)
     
  7. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
  8. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Yea even after trying to test in a scene. It still took ages scratching my head :) lol