- Home /
Car Rotation
Hi guys,
I'm posting cause I looked everywhere to find a solution, tested everything and I didn't find a solution.
I'm making a simple car network, car going from waypoint to waypoint.
My car is just a rigidbody, with sphere colliders on the wheels, they are facing Z and Y is Up.
I let the physic(rigidbody) and gravity tacking care of the X and Z rotations.
Now I want to face the target waypoint, but it seams that I'm not having the good solution, cause sometime the car is going crazy.
I think I would like to end up with an angle to turn in Y, but whatever the rotation in X and Z, I guess this is the solution to after rotate my car from this angle.
Here is the code I have so far:
Thank You for your Help
Vector3 dir = (new Vector3 (targetNode.transform.position.x, targetNode.transform.position.y, targetNode.transform.position.z) - new Vector3 (transform.position.x, transform.position.y, transform.position.z)).normalized;
dir.y = 0;
float direction = Vector3.Dot (dir, transform.right);
var angle = Vector3.Angle (new Vector3 (transform.position.x, 0, transform.position.z), new Vector3 (targetNode.transform.position.x, 0, targetNode.transform.position.z));
Vector3 MyRot = rigidbody.rotation.eulerAngles;
if (direction > .1f) {
rigidbody.rotation = Quaternion.Slerp (rigidbody.rotation, Quaternion.Euler (MyRot.x, MyRot.y + angle, MyRot.z), Time.deltaTime * turnSpeed);
}
if (direction < -.1f) {
rigidbody.rotation = Quaternion.Slerp (rigidbody.rotation, Quaternion.Euler (MyRot.x, MyRot.y - angle, MyRot.z), Time.deltaTime * turnSpeed);
}
//Add Gravity
rigidbody.AddRelativeForce (-Vector3.up * 5000 * Time.deltaTime);
//Add speed and clamp speed
if (rigidbody.velocity.magnitude < speed * .3f)
rigidbody.AddRelativeForce (Vector3.forward * speed * Time.deltaTime, ForceMode.VelocityChange);
Answer by akgdeen · Dec 28, 2011 at 03:08 PM
Below code may help you,
var targetPoint:Vector3 = targetPoint.transform.position;
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 3.0);
rigidbody.AddRelativeForce (0, 0, speed);
Thank you for you answer, but this is what I want to avoid. Cause the car is looking the target point. But I just want the car too point toward the target, meaning just changing the Y rotation, and not the X.
Found it:
Vector3 $$anonymous$$yRot = rigidbody.rotation.eulerAngles; var lookRotation = Quaternion.LookRotation (targetNode.transform.position - transform.position);
rigidbody.rotation = Quaternion.Slerp(rigidbody.rotation, Quaternion.Euler (new Vector3 ($$anonymous$$yRot.x, lookRotation.eulerAngles.y, $$anonymous$$yRot.z)), Time.deltaTime * turnSpeed);
Your answer
Follow this Question
Related Questions
Open world Waypoints NPC and Cars how? 0 Answers
Unity 5 Standard Asset Car Waypoint AI problem with accuracy 0 Answers
How To Add AI To Car/Racer? 4 Answers
How to make an AI of a car to jump if there is any obstacle 2 Answers
car colliding problem 1 Answer