- Home /
Rotate object around transform.forward
Hello! I am trying to rotate an object around it's forward vector. But something is going wrong. This is what I did when the object's rotation is identity and the object has no parent.
• Rotate around local up vector by 90 degrees (now forward vector is (1,0,0) which is correct) • Rotate around local forward vector by 90 degrees (now forward vector should stay the same since I am rotating around the forward vector but it strangely changes to (0,1,0))
This has been blocking my progress quite a lot. Any sort of help will be really appreciated. Here is the code for the rotations. I have also tried it by concatenating quaternion rotations but it gives same result.
private IEnumerator RotateAroundForward (int dir)
{
var t = 0.0f;
var start = transform.localEulerAngles;
var end = start + transform.forward * 90.0f * dir;
while (t < 1.0f)
{
t += Time.deltaTime * rotateSpeed;
transform.localEulerAngles = Vector3.Lerp (start, end, t);
yield return null;
}
}
private IEnumerator RotateAroundUp (int dir)
{
var t = 0.0f;
var start = transform.localEulerAngles;
var end = start + transform.up * 90.0f * dir;
while (t < 1.0f)
{
t += Time.deltaTime * rotateSpeed;
transform.localEulerAngles = Vector3.Lerp (start, end, t);
yield return null;
}
}
private IEnumerator RotateAroundRight (int dir)
{
var t = 0.0f;
var start = transform.localEulerAngles;
var end = start + transform.right * 90.0f * dir;
while (t < 1.0f)
{
t += Time.deltaTime * rotateSpeed;
transform.localEulerAngles = Vector3.Lerp (start, end, t);
yield return null;
}
}
Thanks
Answer by NoseKills · Jul 29, 2016 at 06:15 AM
Maybe my answer to this other question helps
Euler angles are often pretty useless if you do sequences of rotation around more than 1 axis.
Thanks man! This is perfect. Here is the code for anyone who needs this kind of rotation in all directions
private IEnumerator RotateWorld (int dir, Vector3 _axis)
{
var startOrientation = transform.localRotation;
var axis = transform.InverseTransformDirection(_axis);
var end = 90.0f * dir;
var t = 0.0f;
while (t < 1.0f)
{
t += Time.deltaTime * rotateSpeed;
transform.localRotation = startOrientation * Quaternion.AngleAxis($$anonymous$$athf.Lerp (start, end, t), axis);
yield return null;
}
transform.localRotation = startOrientation * Quaternion.AngleAxis(end, axis);
}
Your answer
Follow this Question
Related Questions
Increment Car Rotation along with steering Wheel 0 Answers
Why is this rotation not performed as expected? 1 Answer
How edit transform.forward value ? 0 Answers
Transform.Rotate() rotation about one axis also rotates about the other axes 1 Answer
Eulerangles and Transform.forward... something's off? 1 Answer