- Home /
How would I reduce jittering on this RigidBody2D?
So I have this code where it does what I want by moving the player behind the mouse and having a variable speed depending on how far away it is from the cursor, but it jitters a lot. I don't understand how I would fix this annoying problem and smooth it out, since I'm new to C#. Could it be because of the AddForce? There might be another was to do this that I don't know about. I would be very greatful if you could help me understand this, thanks!
void FixedUpdate () {
//Follow the mouse
var pos = Input.mousePosition;
pos = Camera.main.ScreenToWorldPoint(pos);
var dir = pos - transform.position;
rigidbody2D.AddForce(dir * 4);
}
Your ScreenToWorldPoint may be giving you weird values because of the missing Z value in the position you provide. This may be what's causing your jittering: the direction going up/down the z axis.
And how would I add this z positon? I tried doing "(dir * 4, tansform.postion.z)" but it didn't like that and complained that it's 2D.
Camera.main.ScreenToWorldPoint(new Vector3(pos.x, pos.y, Vector3.Distance(Camera.main.transform.position, transform.position));
Answer by Xysch · Oct 21, 2014 at 08:31 PM
This little bit in a new code fixed it.
void Start () {
rigidbody2D.interpolation = RigidbodyInterpolation2D.Interpolate;
}
Your answer
Follow this Question
Related Questions
How can I add force in the opposite direction of the mouse? C# 0 Answers
Character not rotating 1 Answer
Deteriorating force on object? 0 Answers
How to create a variable jump height 2 Answers
Multiple Cars not working 1 Answer