- Home /
How to make a Cube jump by 1 unit in Z axis
I'm seriously losing my mind with this one, I know it's probably really simple, but all I need is for my cube which is scaled down by half (0.5x0.5x0.5) to jump or leap forward exactly by 1 unit in z axis. This is the method that I tried to use, but It never ends up perfect.
public class PlayerMovement : MonoBehaviour
{
Rigidbody rb;
private void Awake()
{
rb = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
if (Input.GetKeyDown(KeyCode.Mouse0) && Grounded())
{
Jump();
}
}
void Jump()
{
rb.velocity += new Vector3(0, 4.5f, 0) + (1.1362f * transform.forward);
}
bool Grounded()
{
return Physics.Raycast(transform.position, Vector3.down, 0.25f);
}
I used this same method for making cube jump by 2 units and it worked perfectly, (the numbers were different of course), so I thought if I just divide those numbers by 2 i will make it work, but no.
Answer by Boarbox · Jul 23, 2020 at 02:21 AM
I'd recommend using some physics equations.
We know a few things
Gravity, g, -9.81 by default in Unity
The initial vertical speed (in this case 4.5)
The total horizontal displacement (in this case 1)
We can use the following equation to find the amount of time the player will be in the air:
final velocity = initial velocity + g t
0 = 4.5 + -9.8 t
t = 0.45918367346
This is only the time for either rising or falling , so the total time in the air will be double what we just calculated:
t = 0.91836734692
Finally, you know the horizontal distance, and the time in the air, so all you have to do is plug them in for velocity (distance divided by time)
v = x / t = 1 / 0.91836734692
v = 1.08888888891
This is the value you'd use for your horizontal speed
Note that any friction may change the amount of time in the air, so I recommend removing any friction while the player is jumping, just to be safe.
Your answer
Follow this Question
Related Questions
Inconsistent Jump height 3d 3 Answers
How do I make my character jump? 1 Answer
Preventing Rotated Jumping (C#) 1 Answer
Making ball move on drag 0 Answers
How to drag object faster to the ground? 3 Answers