- Home /
Changing jump speed so that different height jumps take exactly the same amount of time?
This is my first question, so I don't know how soon to expect a reply, but I heard that a survey a while back showed that the controls in games featuring jumping were found the most pleasurable in games where, each time the player jumped, he spent the same amount of time in the air each jump. Even though the height could be dependent on the length of time the button was held, the exact time before the player landed again was the same each time.
I was wondering if there was a logical way of making this in JavaScript, so you can calculate the different height jumps' speed to fit a time. Anyone who knows/can work out how to do this, I would very much appreciate if somebody could give me a hand with this, as I have no idea how to go about altering speeds to suit an amount of time.
I am currently using a cube with a character controller attached to run my script, which currently features no height alteration in the jump, as I wanted to do it with a fixed time involved. :) Thanks in advance :D
var jumpSpeed = 9.0f;
var gravity = 14.0f;
var maxFallSpeed = -4.0f;
var canJump : boolean = true;
private var controller : CharacterController;
private var moveDirection = Vector3.zero;
function Update()
{
controller = gameObject.GetComponent(CharacterController);
if (controller.isGrounded)
{
if (Input.GetButton("Jump"))
{
if (canJump == true)
{
moveDirection.y = jumpSpeed;
canJump = false;
}
}
}
else
{
if (moveDirection.y > maxFallSpeed)
{
moveDirection.y -= gravity * Time.deltaTime;
}
}
if (Input.GetButtonUp("Jump"))
{
canJump = true;
}
controller.Move(moveDirection * Time.deltaTime);
}
In order to answer this question you need to spell out 1) how you are moving things in your app (CharacterController, Transform, or Rigidbody), what your current jump code looks like for that type of movement, and how the user would cause the object to jump to different heights.
well, just my opinion, but this is quite difficult, as you can not know how long the player will press the button in advance. but you can increase velocity as long as the button is pressed, decrease it to zero until jumpTime / 2, decrease speed by average on fall. And you can define until when the speed will increase, such as jumpTime / 4...
if(jumping){
counter += Time.deltaTime;
if(Input.GetButton("Jump") && counter < jumpTime / 4f){
rigidbody.velocity += new Vector3(0f, jumpAcceleration * Time.deltaTime, 0f);
velocity$$anonymous$$ax = rigidbody.velocity;
} else if (counter < jumpTime / 2f){
rigidbody.velocity -= new Vector3(0f,((jumpTime / 2f) - counter) * velocity$$anonymous$$ax * Time.deltaTime, 0f);
jumpHeight = transform.position.y - beforeJump.y;
} else if (counter >= jumpTime / 2f){
rigidbody.velocity -= new Vector3(0f, 2f * jumpHeight / $$anonymous$$athf.Pow(jumpTime / 2f), 2)* Time.deltaTime, 0f);
}
}
Please note the above will not work properly and have problems, needs quite modification, the player will jump again if you press jump twice for example, and actual jumpTime will be a little different from what you define, due to delta time. But i think it might give you a general idea.
Of course someone smarter than me will come up with something better, that's how i would do.
Also below link is a useful one for proper calculation of velocity.
Thanks a lot, koray1396 :D I shall have a go at making that script work and post a solution here if/when I can make it work. :)