- Home /
rigidbody2D.AddForce() to a character gives jerky motion
Hi all,
I am adding the force to a simple sprite having rigidbody2d component. Sprite mass is 1, gravity scale is 1. I don't have too much of code and no such BG's just a simple ground and a character in the scene. FPS I am getting on PC 55-60 and on mobile 45-50. Still, while jumping, I found the character too jerky when in motion. I tried with target FPS, but didn't work. Simple code I am using is
void FixedUpdate ()
{
if(Input.GetKeyDown(KeyCode.Mouse0))
{
rigidbody2D.AddForce(Vector2.up*400f);
}
}
Answer by meta87 · Dec 06, 2013 at 10:41 PM
Try turning on interpolation in the Rigidbody2D component. I'm having the same issue. Unfortunately interpolation is behaving very strangely for me, but it might work in your case!
Answer by Everspace · Dec 06, 2013 at 11:23 PM
Try moving the code to Update()
. Because it's in fixed update, it doesn't get applied every frame so it looks jerky/feels wrong.
Physics forces should never be applied in Update
http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.FixedUpdate.html
Remember that never is different than should. When dealing with the player, you are already gating by the input, and input should be dealt with in update. The player could bog down if physics are going wacky. The recommendation is just so you don't end up with something super crazy by adding force every frame.
I have already tried both the methods but didn't work..
If you look at the 2d Tower Defense demo created by Unity, Everspace, (PlayerControl.cs specifically) You will see that Input and all physics are handled in FixedUpdate. They also have Interpolation turned on and the hero gameobject is very smooth.
Sorry to hear it's still not working hirenkacha. I will post if I come up with any answers.