- Home /
Gravity trouble: Falling slow, jumping fast
I'm a noob so go easy on me.
I'm trying to make a simple jump function. Unfortunately, I go up very quickly and come down very slowly. Code is this:
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0F, moveVertical);
//Note I'm not adding anything to the y vector here ^
rb.velocity = movement * speed;
if (Input.GetKey("space") && detectGround())
{
rb.AddForce(0.0F, 1000, 0.0F);
//It seems like I'm adding way too much force on y vector, but even with that much I barely leave the ground. I need, like, 10,000 to get a good high jump.
}
}
bool detectGround()
{
Vector3 down = transform.TransformDirection(Vector3.down);
return Physics.Raycast(transform.position, down, .51F);
}
I'm aware that I might run into weird gravity problems if I'm using odd scales, but I'm dealing with a 1x1x1 sphere in this case. Drag is also not the problem (and if it was, why do I go up so fast?). I've also tried adding some weight to my gravity by changing my y vector. That makes me fall faster but doesn't help the jump any.
Interestingly, I don't seem to have this problem if I use rb.AddForce(movement*speed) instead of rb.velocity = movement*speed. Then, however, I don't get the tight control that I want for my X axis and Z axis. (I accelerate too slowly, never hit a peak speed, and have trouble stopping).
How do I fix this?
if your new i would suggest a movement tutorial this one helped me the best its 12 episodes vary good vary clear explains a lot
https://www.youtube.com/watch?v=$$anonymous$$bW$$anonymous$$8bCAU2w
sorry i can't figure out how to solve your problem myself
Answer by NoseKills · Feb 07, 2017 at 04:38 PM
The problem should be here
Vector3 movement = new Vector3(moveHorizontal, 0.0F, moveVertical);
rb.velocity = movement * speed;
Adding a force to a rigidbody changes its velocity but that velocity only applies for 1 FixedUpdate because you specifically set the velocity.y component to 0 every FixedUpdate. Adding a huge force works because that causes velocity to change a lot so your character ends up high in the air in one FixedUpdate before you override the velocity again.
The solution is not to change the velocity.y.
Vector3 movement = new Vector3(moveHorizontal, 0.0F, moveVertical) * speed;
movement.y = rb.velocity.y;
rb.velocity = movement;
thanks this solved my issue! To any that follow make sure rb.velocity=movement comes after movement.y = rb.velocity.y.
Your answer
Follow this Question
Related Questions
Gravitational Object Creation for 3D Game 1 Answer
Impulse to reach certain height? 1 Answer
Anti-Gravity isn't working 1 Answer
Trying to limit air velocity 2 Answers