- Home /
Choppy movement when rotating
I have an object that's flying. I am using mouse input to move it around the screen, and i use rigidbody.AddForce() to do so. This works absolutely fine.
However, there is a problem. To rotate it towards where i click, i do the following:
When i click:
_targetLocation = targetLocation;
_originRotation = transform.rotation;
_remainingTurn = 0.0f;
_moving = true;
And then each update:
if( _remainingTurn < 1.0f )
{
_remainingTurn += Time.deltaTime * turnRate;
_targetRotation = Quaternion.LookRotation(_targetLocation - transform.position, Vector3.up);
transform.rotation = Quaternion.Slerp(_originRotation, _targetRotation, _remainingTurn );
}
if( _moving && rigidbody.velocity.magnitude < Mathf.Min(maxSpeed, distanceToTarget * 2) && distanceToTarget > movementThresholdDistance )
{
rigidbody.AddForce( (_targetLocation - transform.position) / distanceToTarget * acceleration * Time.deltaTime);
}
This makes my object rotate towards where i clicked quite nicely, but for some reason the entire object moves very choppily as long as it's rotating. I tried changing the rotation line to just set the rotation to the target rotation, but even then, without the Slerp, it moves very choppily as long as _remainingTurn is lower than 1.
Any idea what causes this?
I am using LateUpdate to make the camera follow, but not for moving or rotating the object, no.
Answer by Pwntheon · Feb 22, 2012 at 03:28 PM
I fixed the problem. It's solved by moving everything to FixedUpdate.
Answer by senad · Feb 22, 2012 at 09:48 AM
I think this might be because you are combining physics and setting the transform directly.
Maybe you should try using Rigidbody.AddTorque to rotate your object via physics instead of setting transform.rotation.
I was thinking this as well, but i have no idea how to use AddTorque to make the dragon point where i want, if you catch my drift.
Hey, great I see it works for you now. Although this is probably useless info now, I just want to complete the answer:
I have not used the physics system myself, but in the doku it says you specify a rotation axis and the rigidbody will rotate around it: http://unity3d.com/support/documentation/ScriptReference/Rigidbody.AddTorque.html
So if you think about your current direction and your desired direction as lying inside a plane, the rotation axis should the normal of that plane. (perpendicular to it)
Your answer
![](https://koobas.hobune.stream/wayback/20220613064317im_/https://answers.unity.com/themes/thub/images/avi.jpg)