Jumping only ONCE! ... impossible?

Ok i know this might be a stupid question but ive been trying to get this to work, for example

The player would Jump just like normal with spacebar, but if you walk forward (Hold UP) then press the spacebar to jump you would jump but you can constantly keep doing it till your position is out of the planet..

heres my attempt:

` if (Input.GetButton ("Jump") && rigidbody.velocity.y == 0)

{

rigidbody.velocity = Vector3(0,5,0);

}

if (Input.GetAxis ("Up") && Input.GetButtonDown ("Jump") && rigidbody.velocity.y > 3)

{

rigidbody.velocity = Vector3(0,0,0);

}

else

if (Input.GetAxis ("Up") && Input.GetButtonDown ("Jump") && rigidbody.velocity.y < 0)

{

rigidbody.velocity = Vector3(0,5,0); `

Are you using a rigidbody or characterController.

2 Answers

2

Of course it's possible. For reference, take a look at the FPS demo.

There are several ways you can do this. If your terrain is flat you can put an

if (transform.position.y == 0)

if you are using a charactercontroller use the isGrounded:

function Update () {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded)
    print("We are grounded");
}

else you can use a raycast, cast it downwards and see when it intersects with the plane. If the distance is less then x you can jump.

Trust me, very little is impossible in Unity. :)

... But it doesn't take much to run into impossibilities either. :) For example I haven't been able for the love of my life to add extra vertex data to meshes besides the normal, uvs, color and the usual suspects.

// nevermind i was using rigidbody instead of transform.position.... thats the reason it didnt work

^^ its great little things can be so confusing thanks for the help guys