- Home /
Vector Lerp make weird things
public class CameraRotation : MonoBehaviour {
public Transform camera;
public Vector3 vector;
public float rotateValue;
public float smoothSpeed = 0.125f;
void LateUpdate()
{
vector = new Vector3(0f, 0f, rotateValue);
Vector3 smoothRotation = Vector3.Lerp(transform.eulerAngles, end,smoothSpeed *
Time.deltaTime);
if (Input.GetKey("a"))
{
transform.eulerAngles = -smoothRotation;
}
else if(Input.GetKey("d"))
{
transform.eulerAngles = smoothRotation;
}
else
{
transform.eulerAngles = Vector3.zero;
}
}
}
I tried to Lerp the camera angle on the z axis. But it works only in positive direction. If the value gets negative it turns to a weird spot.
Answer by RLin · Jan 14 at 01:09 AM
Vector3.Lerp linearly interpolates the x, y, and z components of the vector, but this doesn't account for wraparounds from 360 degrees to 0 degrees (otherwise, position interpolation would be messed up).
To interpolate angles, you'll want to work with Quaternions and Quaternion.Lerp instead. See Quaternion.Euler for how to convert a Vector3 euler angle into a quaternion.
Right. I also suggest reading the documentation of eulerAngles. Specifically
When using the .eulerAngles property to set a rotation, it is important to understand that although you are providing X, Y, and Z rotation values to describe your rotation, those values are not stored in the rotation. Instead, the X, Y & Z values are converted to the Quaternion's internal format.
When you read the .eulerAngles property, Unity converts the Quaternion's internal representation of the rotation to Euler angles. Because, there is more than one way to represent any given rotation using Euler angles, the values you read back out may be quite different from the values you assigned. This can cause confusion if you are trying to gradually increment the values to produce animation.