Hello, I have a script, that controls both the gravity and the jumping. I am not using anything like character controller or anything, and it is a simple
function Jump () {
if(down){
rigidbody.AddForce(transform.up * 800 * 10 * 8 * Time.deltaTime);
}else{
rigidbody.AddForce(transform.up * -800 * 10 * 8 * Time.deltaTime);
}
}
function Gravity () {
if(down){
rigidbody.AddRelativeForce(transform.up * -10 * 10 * 8 * Time.deltaTime);
}else{
rigidbody.AddRelativeForce(transform.up * 10 * 10 * 8 * Time.deltaTime);
}
}
The problem is that the jump will sometimes be super short or really high. I normally would have simply used rigidbody for gravity but the game focuses on flipping gravity.
Amon
April 17, 2014, 9:55pm
2
#pragma strict
@HideInInspector
var Gravity : float;
@HideInInspector
var JumpHeight : float;
@HideInInspector
var CanJump : int;
@HideInInspector
var falling : int;
@HideInInspector
var IsJumping : int;
@HideInInspector
var DoJump : int;
function Start () {
CanJump = 1;
Gravity = 3.0;
JumpHeight = 1.0;
falling = 0;
IsJumping = 0;
DoJump = 0;
}
function Update () {
}
function FixedUpdate () {
ActivateJump();
}
function ActivateJump() {
if ( Input.GetKey(KeyCode.Space) && CanJump == 1 ) {
if ( IsJumping == 0 ){
IsJumping = 1;
CanJump = 0;
DoJump = 1;
}
}
if ( DoJump == 1 ) {
Jump();
}
}
function Jump() {
transform.position.y += JumpHeight;
JumpHeight = JumpHeight - Gravity * Time.deltaTime;
if ( JumpHeight < 0 ) {
falling = 1;
//do Collision checks when falling;
//when you hit floor or platform reset everything
FloorCollisionCheck();
}
}
function FloorCollisionCheck() {
//just s imple function to test if the y location is below 0.
// If it is then we stop and reset jumping;
if ( transform.position.y <= 0 && falling == 1) {
falling = 0;
CanJump = 1;
JumpHeight = 1.0;
IsJumping = 0;
DoJump = 0;
}
}
Don’t use too high a value for JumpHeight or too low a value for Gravity when multiplying by Time.DeltaTime.
Yes it doesn’t use built-in physics stuff in unity but it should put you on the correct track.