- Home /
3D Game-Making a character jump
I've tried various ways. I can make my character go up, just not come back down. Also, I want that acceleration effect when jump, so it looks lees like walking upward. The only way I can think of doing it would be too primitive for Unity, which is adding collision for each object, and using some sort of time or jump height for coming back down. ANy help appreciated, or better yet, some snippet of javascript jump code. I would prefer not to use the axis, and just jump from the "ground" my character is on. Here's some code snipped:
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded){
//if(Input.GetKey(KeyCode.Space)){
//transform.Translate(Vector3.up * jumpSpeed * Time.deltaTime);
//transform.position += transform.up * jumpSpeed * Time.deltaTime;
//}
if (Input.GetKeyDown ("space")){
transform.Translate(Vector3.up * jumpSpeed * Time.deltaTime, Space.World);
}
}
If you are using a CharacterController, see the sample script in the reference for CharacterController.$$anonymous$$ove() and ChacterController.$$anonymous$$oveSimple(). Both implement jump using a character controller.
You could add velocity in y direction to the rigidbody. This way the character will go up and through the gravity the velocity will be smaller until it reaches 0 and goes below it which will cause it to fall down.
Answer by Bojidar · Jul 18, 2014 at 03:24 PM
Using Zodiarc's coment:
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded){
if (Input.GetKeyDown ("space")){
rigidbody.AddForce (Vector3.up);
}
}
For this to work, you need to have a rigidbody(3d) attached to your character.
And just a side note make sure to have gravity on after you add the rigidbody, otherwise the character will never come down!
Answer by sixShOOter50o0 · Jul 19, 2014 at 07:23 PM
add this to the code:
var customGravity : float = 10;
var isGrounded : boolean = true;
function Update()
{
if(isGrounded == false)
{
transform.position -= Vector3.up * customGravity * Time.deltaTime;
}
if(isGrounded)
{
//do nothing
}
}
function OnCollisionStay ()
{
isGrounded = true;
}
function OnCollisionExit()
{
isGrounded = false;
}
what this basically does is that if you are not grounded (on the floor) you will apply an opposite force on the y axis which is gravity; and when you are on the floor it will do nothing. in small words you are just emulating gravity. I have not tried this script so i do not know if it will work. you may have a bug that gravity won`t apply when you are touching a wall or anything else that is vertical.
Your answer
Follow this Question
Related Questions
I have a problem with my jump script 1 Answer
3D coyote time 1 Answer
character controller wont rise when I Jump :( 1 Answer
How do I make my character jump? 1 Answer
How to position the jump animation with the object's body? 1 Answer