- Home /
Movement script
I'm trying to wrap my mind around the Platformer controller script from the 2d Platformer tutorial, but I have some doubts I don't seem to be able to solve by reading the script and reference manuals. Here goes. On this piece of code:
function ApplyJumping () { // Prevent jumping too fast after each other if (jump.lastTime + jump.repeatTime > Time.time) return;
if (controller.isGrounded) { // Jump // - Only when pressing the button down // - With a timeout so you can press the button slightly before landing
if (jump.enabled && Time.time < jump.lastButtonTime + jump.timeout) { movement.verticalSpeed = CalculateJumpVerticalSpeed (jump.height); movement.inAirVelocity = lastPlatformVelocity; SendMessage ("DidJump", SendMessageOptions.DontRequireReceiver); } }
}
I don't get what the
if (jump.enabled && Time.time < jump.lastButtonTime + jump.timeout)
bit does. The variables are pretty self-explanatory, (jump is a custom class with a number of variables, timeout is a constant previously set at 0.15)
To me this looks as if jumping will only occur if the button is pressed before timeout time has passed after it was pressed last time. Which doesn't make sense (I can only jump if I keep jumping?). I'm sure I'm missing something, I just can't find out.
Please let me know if you need more details.
Answer by Uzquiano · Apr 18, 2011 at 11:06 AM
Well, you are right, take a look t othe comment ' With a timeout so you can press the button slightly before landing' so in case you are not still grounded you can jump again...
but i am sure there is another part of code where you can jump when you are grounded :)
Yes, but this code seems to apply if you're grounded. (See the if (controller.isGrounded)), so how does that comment make sense?
Yep, sorry I didn't explian myself properly, if you are grounded but if you pressed before landing, the pressed button is still efective, so you can jump again...
Did I explain me better ?... Think about yourself playing games... you press jumpling before landing... but the character doesn't jusmp til grounded...
O$$anonymous$$, so if timeout hasn't yet passed, you can press 'jump' again. But not all jumps last the same (if you hold jump, you jump higher), and timeout is still the same. Does that mean that for longer jumps, the button becomes active earlier?. Also, I've searched the cript and found no other call to the DidJump or CalculateJumpVerticalSpeed functions. The game works though...
Still not sure about this. I can't find any other function that performs the jumping, apart from this, and I can't see how this one does the job...
Answer by oquendo · Apr 18, 2011 at 02:44 PM
I found this, which I think is what applies the initial upwards speed to start the jump (in particular the else if statement).
if (extraPowerJump)
return;
else if (controller.isGrounded)
movement.verticalSpeed = -movement.gravity * Time.deltaTime;
else
movement.verticalSpeed -= movement.gravity * Time.deltaTime;
However, I still don't understand the minus sign?
-movement.gravity * Time.deltaTime
Wouldn't that result in a downwards speed? (gravity is stated at the top of the script as
var gravity = 60.0;
)
Your answer
![](https://koobas.hobune.stream/wayback/20220613123511im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
funky rigidbody movement 0 Answers
The referenced script on this behavior is missing! 2 Answers
Variable Script "movement" 2 Answers
Movement in my terrain ? 2 Answers
Airplane Script Control 1 Answer