- Home /
how to rotate car to camera direction using wheel colliders.
I want to move and rotate car toward camera direction, which is orbiting my car, my car is moving through vertical and horizontal keys, and my camera is orbiting my car through swipe touch. my camera is following my car, but when I change my camera direction, the car is moving straight, I want my car to move and rotate towards camera direction when changing my camera direction.
Actually i want to make control like this: https://www.youtube.com/watch?v=jWCBw6YOPB4&ab_channel=SIBASANKARMURMU
What kind of game play is this? You say orbiting so im guessing its a 3D game, GTA-ish kinda camera and by swiping the screen, you can orbit the camera around the car.
If this is the case, you could subtract the cameras x-y position from the cars x-y position to get a direction vector from the camera to the car. By comparing this to cars forward vector, you can calculate the angle between the camera and the car. Depending on the angle, you could decide to steer left and right to align yourself with the camera, a.k.a. write some code to make the angle between the camera and the car as close to 0 as possible.
thanks for your response, i will try these things, Actually i want to make something like this: https://www.youtube.com/watch?v=jWCBw6YOPB4&ab_channel=SIBASANKARMURMU
Answer by JonnyHilly · Dec 18, 2021 at 02:23 AM
This will be very tricky, due to needing to predict how much steering angle is required to turn smoothly towards a moving target of the camera without going past the current camera angle... and also taking into account any current input and change in the camera's facing direction. Most games would do it the other way around, have your swipe delta +-X control the steering position of the wheels, and then have the camera follow the car and look at the car's position. camera.transform.LookAt( car.position)... // or something similar...
there are existing unity scripts for smoothly following characters ( any object ) for example... https://www.youtube.com/watch?v=MFQhpwc6cKE
If you really want to try doing it the hard way.... start with the transform.forward vectors of both the camera and the car. zero out the Y component so the vectors are flat along the ground plane and normalize them. Now use crossproduct of the 2 vectors to determine the angle between the vectors ( how much you need to turn ) and use the cross ( vector ) product of the 2 vectors to determine which way to turn ( left or right ). but with this technique once you get the basic turn left/right working... you are likely to have a car that turns too far all the time and oscillates back and forth, searching for the correct angle. The math is tricky.
Your answer
Follow this Question
Related Questions
drift car using rigidbody 1 Answer
Destroyed car styled need for speed? 0 Answers
simple drift car movement 0 Answers
How to make traction control for car? 1 Answer
Shift gear with own RPM? Help please! 0 Answers