- Home /
Quaternions, Eulers, and SmoothDampAngle
Hello everyone, glad to be on board here. Here's the issue: I have a cylinder that I need to move from a starting position/rotation to a new position/rotation over time. I've done this kind of thing in the past, but this time I've decided to take advantage of Unity's built in functionality. However, I'm new to Unity so I'm having trouble wrapping my head around the whole Quaternion/Euler thing. I've tried to start off just working with the rotation, and here's what I have so far:
private GameObject myCylinder; // this gets initalized in Start()
private Transform endCylinderTransform; // this holds the angle I want to rotate to
void SetupCylinderInfo()
{
// this sets up the cylinders starting rotation
Vector3 startingRotation = new Vector3(90.0f, 0.0f, 0.0f);
myCylinder.transform.rotation = Quaternion.Euler(startingRotation);
// I want the cylinder to rotate along the x-axis from 90 to 270 degrees
endCylinderTransform = (Transform)Instantiate(myCylinder.transform);
Vector3 endCylinderRot = new Vector3(270.0f, 0.0f, 0.0f);
endCylinderTransform.rotation = Quaternion.Euler(endCylinderRot);
}
Below is the function where I try to do the rotation
float xVel = 0.0f;
void FixedUpdate()
{
// get the angle using SmoothDampAngle. I want to rotate it over 1 second
float xAngle = Mathf.SmoothDampAngle(myCylinder.transform.eulerAngles.x,
endCylinderTransform.eulerAngles.x, ref xVel, 1f);
Vector3 newRot = new Vector3(xAngle, 0.0f, 0.0f);
myCylinder.transform.rotation = Quaternion.Euler(newRot);
}
Well, needless to say this isn't working. It rotates the cylinder a tiny bit but once it reaches it's end it bounces back and forth (probably because I'm not checking for the clamp). Can anyone tell me what exactly I'm doing wrong? I hope that once I get this sorted out I'll be able to deal with the translation in a similar way but first things first.
Thank you. If you need any other info please let me know. -Mo