I am recreating my friends game, and am having trouble with the jump. For some reason, the jump timer isn't being set at all and is instead lasting longer than defined, I cant figure out why its happening. The jump animation loops for a while, then stops randomly. Here is a video of my problem:
Here is my script:
https://hastebin.com/share/efecitoxag.csharp.
The console prints this:
Here is the player inspector:
Here is the jump timer inspector:
void Jump() {
// Detects if the player presses the jump key and is on the floor.
if (jump && IsOnFloor()) {
jumping = true;
// Sets the Y-Velocity with the jump speed.
velocity.Y = -jumpSpeed;
/*/
Adds half of the difference between the max and base jump speed to the current jump speed
Essentially creating 3 jump heights
/*/
jumpSpeed += (maxJumpSpeed - baseJumpSpeed)/2;
// Jump Time
jumpTimer.Start(jumpTime);
// Adds half of the difference between the maxJumpTime and baseJumpTime to the current jumpTime
jumpTime += (maxJumpTime - baseJumpTime)/2;
// Clamps jumpSpeed and jumpTime to the max values
if (jumpSpeed > maxJumpSpeed)
jumpSpeed = maxJumpSpeed;
if (jumpTimer.WaitTime > maxJumpTime)
jumpTimer.WaitTime = maxJumpTime;
}
// Resets the jumpSpeed and jumpTime to default values.
if (!Input.IsKeyPressed(Key.Space))
jumpSpeed = baseJumpSpeed;
jumpTimer.WaitTime = baseJumpTime;
}
here as you can see while you're increasing the jump timer you are also equalising it to the current max value which is always going to be slightly random by the length of the key pressed and not pressed. If you want to add a max jump distance you don't actually need to add a timer for it or different values and max it to the clamp, you can do this
Jump(){
if (Input.IsActionPressed("ui_up") && IsOnFloor()) {
character.velocity.y += max_Jump_Speed*delta value ;
This will allow it to only go up and above against gravity when the key is held so the character will lose momentum to the gravity again and the delta value is 0.016 this will allow the game to run 60fps which is quite slower than usual handling +=1 so you'll need to increase your max velocity hope this helps.
Philippine I'm not trying to add a max jump distance with the timer. I am using the timer to check if the 'jump' has finished, so the amount of time the jump takes to complete should be what the timer is set to. For example, if I jump once, the timer should complete once I hit the ground, without me stopping it manually.
Merill in that case you could just waitTime = 0 when it is on floor and is not jumping and the other code that are setting it to some certain value aren't active states when doing so I'm not sure why would you want that but hope that helps.
or if I understand this correctly if you want to countdown the airborne time you could do that by
if (onfloor == false) {
start timer;
waittime += 1;
}
else {
print(waittime) or assign it to a variable here;
}
© 2022 - 2024 — McMap. All rights reserved.