How do I add in jumping to the Roll-A-Ball tutorial?

Hello, I recently downloaded the personal edition of Unity 5, and followed the Roll-A-Ball tutorial to the end. I feel it’s missing something, which is jumping.
I can not figure out how to add it in, here is the code that I have for the Player object.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Player : MonoBehaviour
{
    public float speed;
    public GUIText countText;
    private int count;
    public GUIText wintext;

    void Start ()
    {
        count = 0;
        SetCountText ();
        wintext.text = "";
    }

    void FixedUpdate ()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");
      
        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
      
        GetComponent<Rigidbody>().AddForce (movement * speed * Time.deltaTime);

}
    void OnTriggerEnter(Collider other)
    {
        if(other.gameObject.tag == "Pickup")
        {
            other.gameObject.SetActive(false);
            count = count +1;
            SetCountText ();
    }
    }

    void SetCountText ()
    {
        countText.text = "Count: " + count.ToString();
        if (count >= 16)
        {
            wintext.text = "You Win!";

        }
    }
}

I would like to be able to jump with the space bar.
Any help is appreciated.
Thanks :slight_smile:

I am new to Unity myself but I think if you either create a new movemnt trigger with a value in the y axis ( where it is currently 0.0f then it would cause the ball to jump. But I would know how to assign it to a key, just a guess ( if I am wrong I am sure someone will correct it). I am following the same tutorial, but got stuck at the GetComponent<Rigidbody>().AddForce(movement *speed*Time.deltaTime); bit due to the change. Is the movementspeedTime.deltaTime something you added in or was it necessary to make it work? if it was necessary do you have a link that explains it?

Thanks

Hi! I’m new to Unity and after reading your post I felt the same thing… so it started bothering me and I spent a bit of time trying to what CoolSpot suggested.

I opened my old Roll The Ball project earlier and added the following in the Update() function:

Please see EDIT below!

if (Input.GetKeyDown ("space")) {
     Vector3 jump = newVector3 (0.0f, 200.0f, 0.0f);
     GetComponent<Rigidbody>().AddForce (jump);
}

And it works just fine for me! xD The 200.0f was just a trial and error for me, I’m sure someone who knows physics can explain to you how to derive a reasonable value for it (1000.0f was too much and 10.0f was barely noticeable).

It’s worth noting that you can also jump while moving, too, and it doesn’t stop you from moving in the direction you’re already moving in before the jump. I think it’s because the movement is added to the sphere as a force on it.

I hope that works for you, too!

EDIT:
I tested it again and realised a big problem! With the script above you can actually jump when you are not actually touching the ground; as a result, you can fly if you keep pressing spacebar! This should help fix the problem assuming you are using the default y position from the tutorial, which is 0.5, if you don’t intend to fly!

voidUpdate () {
     if (Input.GetKeyDown ("space") && GetComponent<Rigidbody>().transform.position.y <= 0.5f) {
     Vector3jump = newVector3 (0.0f, 200.0f, 0.0f);

     GetComponent<Rigidbody>().AddForce (jump)
     }
}

Actually, Unity auto corrected it for me.

That code seems like it would work, but I’m getting parsing errors for some reason. I tried to fix it, no luck.

Oh. D: I wonder if it has anything to do with Unity 5? I updated to Unity 5 a couple of days ago and also let it auto-convert my script when I opened it earlier. I checked the tutorial video just now and I think it has to do with how the rigidbody component is called in Unity 5. If you are not using Unity 5, maybe you could try this:

voidUpdate () {
     if (Input.GetKeyDown ("space") && rigidbody.transform.position.y <= 0.5f) {
          Vector3jump = newVector3 (0.0f, 200.0f, 0.0f);

          rigidbody.AddForce (jump);
     }
 }

if that doesn’t fix the problem then I have absolutely no clue what’s wrong. D:

I am using unity 5.
I’ll continue trying to fix the parsing errors.

Edit:

I got it to work :smile: I had to make it a part of my FixedUpdate thing. Here’s the code

void FixedUpdate ()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");
   
        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
   
        GetComponent<Rigidbody>().AddForce (movement * speed * Time.deltaTime);

        {
            if (Input.GetKeyDown ("space") && GetComponent<Rigidbody>().transform.position.y <= 0.6250001f) {
                Vector3 jump = new Vector3 (0.0f, 200.0f, 0.0f);
           
                GetComponent<Rigidbody>().AddForce (jump);
            }
        }
   
    }

Thanks for all the help :slight_smile:
Now all I have to do is make the jump height into a float, so I can edit it in the editor. That should be easy enough.

1 Like

Thanks for that - it’s all helping the learning curve :slight_smile:

U_nitty

Whaa! I completely missed your message because it came as an edit after I read that you’re fighting with parsing errors! I’m glad that it worked out for you. To be honest, it’s kind of exciting to know that there are other people out there working their way through the same problems that I have! I hope you have started thinking about your first small project!

Good luck! It’s amazing how much you can do in just a few days if you go through the tutorial diligently! I was already really happy after I learned how to make things move. d:

I just figured out how to fix a minor bug. That being endless falling.

void Update ()
    {

        {
            if (transform.position.y <= -10.0f)
            {
                Application.LoadLevel (Application.loadedLevel);
            }
        }
    }

There’s this edit to the code, if you wanted to restart with a button.

    void Update ()
    {

        {
            if (Input.GetKeyDown (KeyCode.R))
            {
                Application.LoadLevel (Application.loadedLevel);
            }
        }
    }

Sadly, there’s a bug with Application.LoadLevel (Application.loadedLevel); But can be fixed, more info can be found here Application.LoadLevel (Application.loadedLevel) resets level but causes light intensity to change - Unity Engine - Unity Discussions

Struggling to learn C# here :frowning:

Are the braces at lines 5 and 10 above superflous ??

U_nitty

I believe they are. xD

I’m not much of a programmer and it’s difficult at the beginning. Just keep going through the tutorials and don’t worry too much if you don’t remember - personally, I find that it’s fine as long as you can recall how to do things (and know where to look).

At the beginning I force myself to do everything from scratch but it was silly because it actually slowed down the learning. Don’t be ashamed of copy and pasting codes you’ve already typed during a tutorial and/or made yourself into a new project! o:

Good luck!

Here is a really good script I use. : )

usingUnityEngine;
usingSystem.Collections;

publicclassWasdCont : MonoBehaviour {
//Variables
publicfloatspeed = 6.0F;
publicfloatjumpSpeed = 8.0F;
publicfloatgravity = 20.0F;
privateVector3moveDirection = Vector3.zero;

voidUpdate() {
CharacterControllercontroller = GetComponent();
//isthecontrollerontheground?
if (controller.isGrounded) {
//FeedmoveDirectionwithinput.
moveDirection = newVector3(Input.GetAxis(“Horizontal”), 0, Input.GetAxis(“Vertical”));
moveDirection = transform.TransformDirection(moveDirection);
//Multiplyitbyspeed.
moveDirection *= speed;
//Jumping
if (Input.GetButton(“Jump”))
moveDirection.y = jumpSpeed;

}
//Applyinggravitytothecontroller
moveDirection.y -= gravity * Time.deltaTime;
//Makingthecharactermove
controller.Move(moveDirection * Time.deltaTime);
}
}

You might have to attach this as an other script.

thankx alot mate
this one really helped me

…i tried this but i get crazy ball rotating all the time and it is going through the ground…i have the character controller made and it is enveloping the ball and it is above the ground…the script is only good when i used kinematic but that is not what we are after here right?..can anyone help?..im totally new in unity scripting

You might need to lower the speed. If it is too fast it will go crazy.

I had the same issue

I set the minimum move distance to 0 and removed the rigidbody component from the ball and that fixed it. hope this helps!

can you not just use collide so like if collide with ground true then able to jump you would have to use different blocks so non child of same parent to do so right?

I am stuck with the code and not able to make the ball jump!! pls, help me out! here is my code…

using UnityEngine;
using System.Collections;

public class BallControl : MonoBehaviour {

public float speed;
private Rigidbody rB;

void Start(){
rB = GetComponent ();
}

void Update(){

}

void FixedUpdate(){
float moveHorizontal = Input.GetAxis (“Horizontal”);
float moveVertical = Input.GetAxis (“Vertical”);

Vector3 movement = new Vector3 (moveHorizontal,0.0f,moveVertical);

rB.AddForce (movementspeedTime.deltaTime);

if(Input.GetKeyDown(“space”) && rB.transform.position.y<=0.6250001f){
Vector3 jump = new Vector3 (0.0f,200.0f,0.0f);
rB.AddForce (jump*Time.deltaTime);
}

}
}