- Home /
HELP!! Rigid Body Jumping Gravity Issue
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.
http://www.youtube.com/watch?v=h3L30zcculE&feature=youtu.be
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?
Answer by Crystalline · Oct 25, 2013 at 08:57 AM
Just: If not jumping and not grounded add a negative force. Attach this script to your rigidbody.
#pragma strict
var distToGround: float;
function Start () {
distToGround = collider.bounds.extents.y;
}
function IsGrounded(): boolean
{
return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1);
}
function FixedUpdate () {
if (IsGrounded())
{
// JUMPING
if (Input.GetKey(KeyCode.Space))
{
transform.rigidbody.AddForce(transform.up* 500);
}
else
{
transform.rigidbody.AddForce(transform.up* -500);
}
}
}
how is unity supposed to know what grounded is?
At the moment, that script makes my character fly away.. I guess my question should be how do I really tell unity that my character is on the ground so that it knows how to respond to these triggers..
you can't put a script on rigid body, though....the player holds that attribute so the script has to be on the player, right?
Your answer
Follow this Question
Related Questions
Double Jump 1 Answer
Character floats upward when looking up and down 0 Answers
Character Controller / Charactor Motor has no inputs? 1 Answer
Grounded Jumping scripting problem. 1 Answer