- Home /
AddForce doesn't work when used with velocity
Hi, I'm very new to Unity and I can't get around this weird problem. I have a top down iOS game where you roll a ball around using the accelerometer and then tap to jump. I'm using rigidbody.AddForce to jump in the Y direction and the rolling movement is on the XZ plane.
What happens is that when I tap, the ball jumps but not that high and it falls to the ground very slowly. This is very odd because if I use rigidbody.AddForce() instead of rigidbody.velocity to roll the ball around, the jump works really well and it also jumps higher than before. Any ideas about what's going on here? I double checked my colliders and Rigidbody settings and all seems fine, and it even works if I don't use rigidbody.velocity, but I feel that's the best way to roll the ball around in the game so I have to use it.
BTW, the reason I use GetMouseButton(0) is so that I can try it out the jump in the Editor with my mouse. Same command accepts iOS touch also.
Any help is appreciated,
This is my code:
moveDirection.x = Input.acceleration.x;
moveDirection.z = Input.acceleration.y;
if (moveDirection.sqrMagnitude > 1f)
{
moveDirection.Normalize();
}
rigidbody.velocity = moveDirection * Time.fixedDeltaTime * moveSpeed * speedFac;
if (Input.GetMouseButton(0))
{
Debug.Log("touched");
if (isGrounded)
{
rigidbody.AddForce(0f, jumpforce * Time.FixedDeltaTime, 0f, ForceMode.Impulse);
isGrounded = false;
}
}
Answer by smokenstein · Mar 17, 2014 at 03:33 PM
I may have solved my Issue (for those interested) - Use AddForce and ForceMode.VelocityChange
I will post a reply once I've tested it but right now that seems like my best option.
Had to raise the drag on the rigidbody and the speed to compensate for that. This made the controls more responsive like it was when I was directly manipulating the velocity. The way I got the Jump to work was by reducing the drag and the speed while the player was jumping, this made the jump look more realistic (otherwise the descend would've been super slow due to the high drag)
Cool! These tips are going to help me with my own game, cause I've been wrestling with the same problems.
Answer by frogsbo · Mar 16, 2014 at 06:41 PM
velecity overrides add force and physics. it's a higher level than physics commands like force. if a button goes down to addforce, then say if addforce down, not set velocity values.
In most cases you should not modify the velocity directly, as this can result in unrealistic behaviour. Don't set the velocity of an object every physics step, this will lead to unrealistic physics simulation. A typical example where you would change the velocity is when jumping in a first person shooter, because you want an immediate change in velocity.
Thanks frogsbo, how would you recommend rolling a ball then? If I use AddForce to roll it, it will only roll in a direction until a strong enough force is applied in a different direction. What velocity gives me is a more responsive change in direction and speed and that's what I really wanted (along with being able to jump). Is there another way to make the ball jump other than AddForce?
Answer by PRABHBIR123 · Jan 27, 2019 at 03:31 PM
If you @smokenstein want more info on this and a better solution then check out this. https://youtu.be/_93c-78CLak
Your answer
Follow this Question
Related Questions
Gravitational Object Creation for 3D Game 1 Answer
Manually Apply Cars Collision Response Force 0 Answers
Calculate velocity to set to reach a target point on a plane considering the drag. 0 Answers
Rigidbody character controller can't walk on stairs 0 Answers
Velocity powered rigidbody on a moving platform without parenting. 3 Answers