- Home /
How to make cars rotate towards a waypoint while turning?
I have cars in my 2D game that have a rigidbody2D and box collider 2D. I was translating/rotating them using their transform and I recently found that you should not move gameObjects having colliders and rigidbody via transform as it recalculates the colliders and is heavy of CPU. So, I am using Rigidbody2D.position and Rigidbody2D.MoveRotation to move them. Setting the position works well but I am having trouble calculating the angle for MoveRotation when then follow waypoints to turn. This is how my code looks: Here is the code I have for the cars, the turn function is called by turn triggers when car collides with them and they pass on their waypoints and indicate if the car should be turning left or right. public float moveSpeed = 20.0f; private bool m_isTurningLeft = false; private bool m_isTurningRight = false; private int m_wayPointIndex = 0; private float m_wayPointStartTime; private float m_timeToWayPoint; private Vector3 m_directionToWayPoint = Vector3.zero; private Vector3 m_startDirection = Vector3.zero; private Vector3 m_startPosition = Vector3.zero; private float m_angle; private Transform [] m_wayPoints;
public void turn(ref Transform [] waypoints , bool turnLeft, bool turnRight) {
m_wayPoints = waypoints;
m_isTurningLeft = turnLeft;
m_isTurningRight = turnRight;
m_wayPointIndex = 0;
m_wayPointStartTime = 0.0f;
m_timeToWayPoint = Vector3.Distance(transform.position, m_waypoints[m_wayPointIndex].position)/moveSpeed;
m_directionToWayPoint = (m_wayPoints[m_wayPointIndex].position - transform.position).normalized;
m_startDirection = transform.up;
m_startPosition = m_rigidBody.position;
// angle between transform.up and the target direction
m_angle = Vector3.Angle(m_startDirection, m_directionToWayPoint);
Vector3 cross = Vector3.Cross(m_startDirection, m_directionToWayPoint);
// if cross product points in +ve z
if(cross.z > 0.0f) {
m_angle = 360.0f - m_rigidBody.rotation - m_angle;
}
void FixedUpdate () {
if(m_isTurningLeft || m_isTurningRight) {
// check if vehicle reached current waypoint
if(m_wayPointStartTime > m_timeToWayPoint) {
m_wayPointIndex++;
// check if this is the last way point
if(m_wayPointIndex == m_wayPoints.Length) {
transform.position = m_wayPoints[m_wayPointIndex - 1].position;
m_wayPointIndex = 0;
if(mIsTurningRight) {
mIsTurningRight = false;
} else if(mIsTurningLeft) {
mIsTurningLeft = false;
}
} else {
m_wayPointStartTime = 0.0f;
m_timeToWayPoint = Vector3.Distance(transform.position, m_wayPoints[m_wayPointIndex].position)/moveSpeed;
m_directionToWayPoint = (m_wayPoints[m_wayPointIndex].position - m_wayPoints[m_wayPointIndex - 1].position).normalized;
m_startDirection = transform.up;
m_startPosition = m_rigidBody.position;
m_angle = Vector3.Angle(m_startDirection, m_directionToWayPoint);
Vector3 cross = Vector3.Cross(m_startDirection, m_directionToWayPoint);
if(cross.z > 0.0f) {
m_angle = 360.0f - m_rigidBody.rotation - m_angle;
}
}
}
else {
// move up towards the current way point
m_wayPointStartTime += Time.fixedDeltaTime;
float percentTime = m_wayPointStartTime/m_timeToWayPoint;
m_rigidBody.position = Vector3.Lerp(m_startPosition, m_wayPoints[m_wayPointIndex].position, percentTime);
}
} else {
// not turning, move the car forward in straight line
m_rigidBody.position = m_rigidBody.position + (new Vector2(transform.up.x, transform.up.y) * moveSpeed * Time.fixedDeltaTime);
}
}
The cars don't face the waypoints correctly while turning and keep rotating in weird angles. Please let me know what I am doing wrong. Thanks in advance.
Your answer
Follow this Question
Related Questions
In unity 2D c# how to rotate an object like geometry dash? 1 Answer
How can I make my character walk along the slope? 1 Answer
Help with translation and rotation on a rigid body 2D 0 Answers
How to limit rotation angle on a rigidbody2d ? 0 Answers
Colliding players spin out of control and rotation is local. Need help! [C#] 0 Answers