- Home /
How to Clamp LookAt on the Y Axis
I have looked for awhile on how to incorporate something like this into my code and just can't figure it out.
So the Code I have is on the two front tires of the vehicle, and the Car is following the Target "Player" which everything is working the wheels turn as well as the wheel Colliders but if the player was to run a circle around the vehicle the wheels would turn a full 360 Degrees which I don't want. and I was wondering how to add a Max steer angle on the y axis so they can only turn at max a 30-45 degree angle.
Here is the code thanks in advance!
Vector3 targetPos = new Vector3(target.transform.position.x, frontDriverT.position.y, target.transform.position.z);
Vector3 targetPos2 = new Vector3(target.transform.position.x, frontPassengerT.position.y, target.transform.position.z);
frontDriverT.LookAt(targetPos);
frontPassengerT.LookAt(targetPos2);
eulerAngY = frontDriverT.localEulerAngles.y;
eulerAngYTwo = frontPassengerT.localEulerAngles.y;
frontDriverW.steerAngle = eulerAngY;
frontPassengerW.steerAngle = eulerAngYTwo;
Answer by Dankey_Kang · Oct 03, 2018 at 07:19 AM
Instead of using LookAt, I would set the transform.forward directly from a calculated vector that can be bounded. I assume you want the wheel bound to be relative to the front of the vehicle. If not then change it to whatever you want the rotation to be relative to, in this case I represented the vehicle transform with vehicleT. You also may have to throw a negative on the quaternion.euler y component depending on if the rotation is flipped.
Vector3 targetVector = new Vector3(target.transform.position.x - frontDriverT.position.x, 0f, target.transform.position.z - frontDriverT.position.z);
Vector3 relativeVector = new Vector3(vehicleT.forward.x, 0f, vehicleT.forward.z);
float wheelAngle = Vector3.SignedAngle(relativeVector, targetVector, Vector3.up);
wheelAngle = Mathf.Clamp(wheelAngle, -maxWheelAngle, maxWheelAngle);
Vector3 wheelForward = Quaternion.Euler(0f, wheelAngle, 0f) * relativeVector;
frontDriverT.forward = wheelForward;
Your answer
Follow this Question
Related Questions
How to make the calculated angle respective of the players rotation? 1 Answer
How to send Slider Information over websocket? 1 Answer
Multiple Cars not working 1 Answer
Smooth movement for Gameobjects switching position 2 Answers
Function to cheack if there is already a gameobject in a indicated vector not working 2 Answers