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
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?
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:
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!
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:
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’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:
…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
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?