Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411613 Posts in 69390 Topics- by 58447 Members - Latest Member: sinsofsven

May 09, 2024, 03:41:12 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Double jump script fail (fixed + unity js example)
Pages: [1]
Print
Author Topic: Double jump script fail (fixed + unity js example)  (Read 3625 times)
deathtotheweird
Guest
« on: November 04, 2009, 12:11:22 PM »

What I wanted to do is reset the jumpcount to zero while grounded (works) and add one one to each jump count whenever the space bar is pressed down and then stop jumping when the character gets over the maximum amount of jumps, two. when i go in the air and press space twice it only adds 1 to the jumpCount and doesn't double jump (I didnt bother to stop it from jumping yet since I can't even get the player to jump twice)

I think I may have to just write it from scratch, I don't think I can double jump with it like it is.

unity 'javascript'

Code:
var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
var jumpCount = 0;
var maxJump = 2;

private var moveDirection = Vector3.zero;
private var grounded : boolean = false;

     //Reset jumpCount
function OnCollisionEnter() {
jumpCount = 0;
}

function Update() {
if (grounded) {
jumpCount = 0;
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if  (Input.GetButtonDown("Jump"))  {
moveDirection.y = jumpSpeed;
jumpCount++;
}

}

// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;

// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;

//Run Toggle
if (Input.GetKey (KeyCode.LeftShift))
speed = 13;

else
speed = 6;
}

@script RequireComponent(CharacterController)
« Last Edit: November 04, 2009, 02:29:12 PM by allen » Logged
powly
Level 4
****



View Profile WWW
« Reply #1 on: November 04, 2009, 12:40:33 PM »

You have your jump code inside the if(grounded) -block, just take it out and it should work fine.

Just seems like a minor typo ;3

Minor edit: I get confused with this kind of stuff easily so I usually indent like this:

Code:
int main(int argc, char* argv[])
{
    int array[10][10];
    for(int x = 0; x<10; x++)
    {
        for(int y = 0; y<10; y++)
        {
            while(array[x][y]<100)
                array[x][y]++;
        }
    }
    return 0;
}

I've always found putting those curve bracets after a statement a little weird.
« Last Edit: November 04, 2009, 12:49:11 PM by msqrt » Logged
deathtotheweird
Guest
« Reply #2 on: November 04, 2009, 01:17:24 PM »

...

ah thanks.

Yeah, I need to work on my formatting. I just get lazy.
Logged
deathtotheweird
Guest
« Reply #3 on: November 04, 2009, 02:32:33 PM »

Ok I fixed it. It's just the FPSWalker script modified, but with double jump and air control

formatting is messy and the code could be tweaked a bit, not many comments

Code:
var speed = 6.0; 
var jumpSpeed = 8.0;
var gravity = 20.0;
var jumpCount = 0;
var maxJump = 2;

private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
   
//Reset jumpCount
function OnCollisionEnter() {
jumpCount = 0;
}
function Update() {
if (!grounded) {
moveDirection.x = Input.GetAxis("Horizontal")*speed;
moveDirection.z = Input.GetAxis("Vertical")*speed;

   }

   else {
      moveDirection = Vector3(Input.GetAxis("Horizontal")*speed, 0.0, Input.GetAxis("Vertical")*speed);
  //reset jump because we are grounded
   jumpCount = 0;
   }
//jump
if (jumpCount < maxJump) {
if (Input.GetButtonDown ("Jump"))  {
         moveDirection.y = jumpSpeed;
jumpCount++;
      }
}
   moveDirection = transform.TransformDirection(moveDirection);

   // Apply gravity
   moveDirection.y -= gravity * Time.deltaTime;
   
   // Move the controller
   grounded = (GetComponent(CharacterController).Move(moveDirection * Time.deltaTime) & CollisionFlags.CollidedBelow) != 0;
   
//shift toggles run
if (Input.GetKey (KeyCode.LeftShift))
speed = 13; //run speed

else //
speed = 6;
}

@script RequireComponent(CharacterController)
Logged
Hideous
That's cool.
Level 10
*****


3D models are the best


View Profile WWW
« Reply #4 on: November 04, 2009, 09:00:06 PM »

But you're using onCollisionEnter - won't this reset the counter if you run into a wall?
Logged

deathtotheweird
Guest
« Reply #5 on: November 04, 2009, 09:37:12 PM »

yeah I forgot to change that for public consumption (in my game i dont have walls so i didnt change it)

if anyone uses it im sure they could figure it out, if not id be more than happy to fix it
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic