ey when i press space i jump but now i want a double jump i treid something but it did not come far
the best thing iv got is that when i jump he automaticly adds the second but thats not what i want…
here is the script for so far…:
function FixedUpdate() {
if (grounded) {
// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(Input.GetAxis(“Horizontal”), 0, Input.GetAxis(“Vertical”));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
hmm well it seems like a bit of a convoluted way of doing it (no offense… i write crazy convoluted shit)
for one thing, try not to use double-negatives in your booleans… you’re just asking to confuse yourself. when you start trying to understand large systems, organization and keeping the components clean, simple, and readable is of prime importance.
I dont think you even need that second nogrounded flag (btw, camel casing is your friend — “noGrounded” )
you should be able to use logic something like this:
if grounded
do grounded logic and if the user hits jump, he jumps and is no longer grounded
if !grounded
apply gravity and
if !hasDoubleJumped
user can hit jump one additional time. that doesn't change his grounded state, that just gives him a boost in the y direction.
oh, one other thing. you’re going to find that having your user input in FixedUpdate is going to cause you a problem or two. there will be times when you hit a key and nothing happens, because it’ll happen between FixedUpdate() ticks. all controls should generally be put into Update()
So I got to playing around and seeing if I could solve this for ya. What I came up with was a bit gut wrenching, but it’s funny to watch…
I put this code onto a simple box and moved it around a terrain. Probably would do better with a capsule, but meh, it’s free.
private var grounded=true;
private var hasDoubleJumped=false;
private var jumpKeyDown=false;
private var doneSecondJump=false;
private var speed=100.0;
private var jumpSpeed=80000;
private var rotateSpeed = 3.0;
function Update() {
// if we dont have the space bar pressed, make sure we can press it in the future.
if (!Input.GetButton ("Jump"))
jumpKeyDown=false;
// do the rotation (all in world space)
transform.RotateAround (transform.position, Vector3.up, Input.GetAxis ("Horizontal") * rotateSpeed); // world space
//transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0); // local space
//flipping around is nice, but lets keep us upright....
transform.eulerAngles=Vector3(0,transform.eulerAngles.y,0);
if (grounded) {
// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
// Clear the jump keys cos we are on the ground.
doneSecondJump=false;
// do the first jump, because we are on the ground.
if (Input.GetButton ("Jump") !jumpKeyDown) {
print("First Jump");
DoJump();
grounded = false;
}
}else{
// if we are in the air, and havent done the second jump, we can. but only once.
if (Input.GetButton ("Jump") !jumpKeyDown !doneSecondJump) {
print("Second Jump");
DoJump();
doneSecondJump=true;
}
// half movement in the air... cos we have wings!!!
moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical")/2);
}
// since we can move in the air, lets do our speed multiplication outside of our grounded stuff.
moveDirection *= speed;
transform.Translate(moveDirection * Time.deltaTime);
}
// I did this because I hate rewriting code... it didnt cut down on the code, but I am happy to write more code, to get around writing more code....
function DoJump(){
rigidbody.AddForce(0,jumpSpeed,0);
jumpKeyDown=true;
}
// if we are on the ground, tell us so.
function OnCollisionStay(collision : Collision) {
// we are only grounded when we have a contact below us... don't hit your head too hard.
var hasGrounded=false;
for (var contact : ContactPoint in collision.contacts) {
if(Vector3.Dot(-Vector3.up,contact.point-transform.position))
hasGrounded=true;
}
grounded=hasGrounded;
}
// if we are not contacting anything, tell us so...
function OnCollisionExit(collisionInfo : Collision) {
grounded=false;
}
I found a slightly simpler way to do a double jump.
var speed : float = 6;
var jumpForce : float = 8;
var gravity : float = 20;
var jumpLimit : int = 2;
private var moveDirection : Vector3 = Vector3.zero;
function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded)
{
//When the player is grounded reset the double jump variable.
jumpLimit = 2;
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
//If the jumpLimit is more than zero allow the player jump.
if (jumpLimit > 0)
{
if (Input.GetButtonDown("Jump"))
{
//Subtract one each time the player presses the jump button.
//Once it reaches zero the player will no longer jump
jumpLimit--;
moveDirection.y = jumpForce;
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}