I'd want to rotate the character along the y-axis (green) of another gameobject.
Here's a picture of my system.
I'd want to rotate the character in the direction indicated by the red arrow (see figure, it is not x axis).
I looked at other questions and tried a lot of code, but I still didn't achieve the desired results.
This is my latest code that created the picture above.
The picture on the right is what I want, but the picture on the left is not.
Any advice is appreciated. Thank you!
IEnumerator RotateToDirection(){
var targetRotation = new Vector3(0, ArrowObject.transform.localEulerAngles.y, 0);
var elapsedTime = 0.0f;
var waitTime = 1f;
while (elapsedTime < waitTime)
{
elapsedTime = Time.deltaTime;
Character.transform.localEulerAngles
= Vector3.Lerp(Character.transform.localEulerAngles, targetDirection, (elapsedTime / waitTime));
yield return null;
}
}
CodePudding user response:
Don't use eulerAngles
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.
From your images it sounds like you want the character's forward axis to align with another objects up
axis so you would probably use e.g. Quaternion.LookRotation
something like e.g.
var startRotation = Character.transform;
var targetDirection = ArrowObject.transform.up;
var targetRotation = Quaternion.LookRotation(targetDirection);
for(var timePassed = 0f; timePassed< 1f; timePassed = Time.deltaTime)
{
Character.transform.rotation = Quaternion.Lerp(startRotation, targetRotation, timePassed / 1f);
yield return null;
}
Character.transform.rotation = targetRotation;