- Home /
Question by
hulahoolgames · Feb 09, 2016 at 10:33 PM ·
rotationquaternion.lookrotation
Issues with rotation at 180 degrees
void FixedUpdate() {
if(m_wayPoints != null) {
if(m_rigidBody.position != m_targetWaypoint.position) {
Vector3 newDir = Vector3.RotateTowards(m_rigidBody.transform.forward, m_targetDirection, rotateSpeed * Time.deltaTime, 0.0f);
m_rigidBody.rotation = Quaternion.LookRotation(newDir, Vector3.forward * -1.0f);
m_rigidBody.position = Vector3.MoveTowards(m_rigidBody.position, m_targetWaypoint.position, moveSpeed * Time.deltaTime);
} else {
m_startPos = m_rigidBody.transform;
// m_targetWaypoint = getRandomWayPoint();
if(m_targetWaypoint == m_wayPoints[0]) {
m_targetWaypoint = m_wayPoints[m_wayPoints.Length - 1];
} else {
m_targetWaypoint = m_wayPoints[0];
}
m_targetDirection = (m_targetWaypoint.position - m_rigidBody.position).normalized;
}
Quaternion newRot = Quaternion.Euler(rotor.transform.localRotation.eulerAngles.x, rotor.transform.localRotation.eulerAngles.y + rotorSpeed * Time.deltaTime, rotor.transform.localRotation.eulerAngles.z);
rotor.transform.localRotation = newRot;
}
}
I have this piece of code that basically moves my object between two waypoints that are exactly opposite. When the object reaches one waypoint and has to turn towards the other, it does a barrel roll kind of rotation, i.e the forward of the object rotates around the local x axis. This is a top down game where the player is looking down from -ve Z direction. Hence my up vector to LookRotation is Vector3.forward * -1.0f. This works fine when the object moves to any other waypoint where the angle it has to rotate is not 180. What am I doing wrong with this? Any help is appreciated. Thanks in advance.
Comment
Your answer
