Reputation: 75
I have 3 objects inside Unity that are 3 segments of an arm that is supposed to move. Each segment has 2 more empty game objects that are placed on the bottom and top of the segments (these are used as joints, which i wanna apply the rotations to). Inside the project these objects look like this:
a1_1 is the main joint at the bottom of the arm and when this rotates everything should rotate with it (thats why its the parent). arm1, arm2 and arm3 are the segments. a2_1 and a3_1 are the two other joints that get rotations added.
Now i have a .txt file with multiple lines of angles for each joint. I already have implemented a way to read this file and I'm using a foreach loop to read each line separately. However when I use the Quaternion.Lerp function it adds weird rotations and I dont really know why.
All I want to do is for the joints to rotate to one specific point wait like 0.2 seconds and then rotate to the next point.
IEnumerator Run() {
string[] tempAngles;
foreach (string line in angleArray) {
tempAngles = line.Split(' ');
a1_1.rotation = Quaternion.Lerp(a1_1.rotation, Quaternion.Euler(90 - float.Parse(tempAngles[1]), float.Parse(tempAngles[0]), 0), Time.fixedDeltaTime * 1f);
a2_1.rotation = Quaternion.Lerp(a2_1.rotation, Quaternion.Euler(180 - float.Parse(tempAngles[2]), 0, 0), Time.fixedDeltaTime * 1f);
a3_1.rotation = Quaternion.Lerp(a3_1.rotation, Quaternion.Euler(float.Parse(tempAngles[3]) + 45, 0, 0), Time.fixedDeltaTime * 1f);
//a1_1.rotation = a1_1.rotation * Quaternion.AngleAxis((90 - float.Parse(tempAngles[1]) * Time.fixedDeltaTime), Vector3.right);
//a1_1.Rotate(90 - float.Parse(tempAngles[1]), float.Parse(tempAngles[0]), 0);
//a2_1.Rotate(180 - float.Parse(tempAngles[2]), 0, 0);
//a3_1.Rotate(180 - float.Parse(tempAngles[3]), 0, 0);
yield return new WaitForSeconds(0.2f);
print("done");
}
}
This is how the function looks so far. As you can see I have tried multiple different ways to add a rotation but none seem to work the way I want them to.
This is how one line of angles look inside the txt file:
62.920341623781205 42.26008063439084 86.66333878215205 141.0765805834571
I split each angle where the spaces are and get 4 angles per step in the foreach loop.
Upvotes: 1
Views: 490
Reputation: 1680
You need to store the initial rotation at the beginning of every lerp in a variable. For example, if you wanna lerp from 30 to 60 degrees, after you reach 40, a1_1.rotation becomes 40, but you still need to put 30 in the first parameter lerp function for it to work properly. https://docs.unity3d.com/ScriptReference/Quaternion.Lerp.html
This is the problem here, but you may or may not also need to use Transform.localRotation
Upvotes: 1