Hey guys. I’m new to Unity, and more so with coding. I’m making a 2D game and I’m having a bit of trouble with the jumping mechanics.
Initially I used the character preset asset package to control my character, but there was an annoying pill collider in it which didn’t work well with this 2D style platformer. So I researched a little and made a code that allows me to control my character without a character controller, and rather a rigid body.
Here is the code:
#pragma strict
var speed : int = 5;
var isgrounded : boolean = true;
function Start () {
}
function Update () {
if(Input.GetAxis("Horizontal")){
transform.Translate(Vector3(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, 0));
}
if(Input.GetKeyDown("space") && isgrounded == true){
rigidbody.AddForce(Vector3.up * 500);
}
}
Here is a video showing what it does (white background). The blue background is what I WANT the character to jump like. I would go on with using that blue-background-code, but the pill collision on that character controller doesn’t work well with what I’m trying to make.
So does anybody know how to tweak the gravity in unity to make the character not only jump up faster, but land fast as well?
I have tried edit > project settings > Input (because there was a gravity option)
I have tried edit > project settings > Physics
The gravity options there don’t make the character jump like shown in the blue background example in the youtube video above. So I was wondering if there was a code I could use on a rigid body to modify gravity. Any help would be greatly appreciated.
!!![[[BONUS HELP]]]!!!
As you can see in the video, the character slides around if the jump key (“space”) is held down. I don’t understand raycasting yet, but I hear that’s a way to fix it, to detect that the character has hit the ground. If someone can explain that, or show me a code that works, I’d be very happy.
&
As you can see from the video, if the jump key is continually tapped, the character floats around like kirby, but limitlessly. I would be fine with a double jump, but not an infinite one. How do I limit the amount of times pressing jump takes any effect?