I have a character that I want to manually animate by rotating joints (LookAt/LookRotation) based on the position of a cube in the scene. For example, I want to rotate the LeftHip transform of this character so that it points to the cube.
Here’s my code:
void Update ()
{
Quaternion q = Quaternion.LookRotation(target.transform.position - this.transform.position , transform.up);
transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * 10f);
/* If rotating using LookAt */
//transform.LookAt(target.transform.position);
}
At first glance, everything works fine. But when I start moving the cube back and forth for a couple of times, the leg starts twisting along the forward axis even if it’s still pointing to the cube. When I use the LookAt function, it starts to twist once I move the cube to the back, and rotates back to the proper position when I move the cube forward.
Any ideas on why the transform is rotating that way?
The thing about ‘look at’ is it doesn’t not completely describe an orientation, you need to specify the orthogonal vector which describes the local ‘up’.
Now, the two methods work almost the same. However, the transform will eventually twist particularly when I randomly move the cube around the scene. Maybe I need to use the initial rotation somewhere in my code. What do you think?