- Home /
How to make a functional steering wheel for a car game?
I want my steering wheel to be like a real one and so I've made it so that you can rotate it left or right but I want to be able to reset the rotation of the wheel once the user has stopped turning the wheel, and I assume I'd need some kind of variable to store the amount of rotation of the wheel, but I don't know how.
Anyways, here's my code so far:
using UnityEngine;
using System.Collections;
public class Wheel : MonoBehaviour {
public int turnSpeed;
public GameObject wheel;
void Update ()
{
if(Input.GetKey (KeyCode.A))
{
wheel.transform.Rotate(-Vector3.up * Time.deltaTime * turnSpeed);
}
if(Input.GetKey (KeyCode.D))
{
wheel.transform.Rotate(Vector3.up * Time.deltaTime * turnSpeed);
}
}
}
You're going to need to store the amount of times it has rotated completely, but eventually Quaternion.RotateTowards will be what you want to get back to the normal position
Resetting the rotation will look pretty unrealistic. Especially if the wheels dont follow or if they snap to place instantly.
Answer by JedBeryll · Dec 01, 2015 at 11:27 PM
Just store the default rotation of the wheel at start to something like:
float rotations = 0;
Quaternion defaultRotation;
void Start()
{
defaultRotation = wheel.transform.localRotation;
}
then you can rotate it back like this:
void Update()
{
if(Input.GetKey (KeyCode.A))
{
wheel.transform.Rotate(-Vector3.up * Time.deltaTime * turnSpeed);
rotations -= turnSpeed;
}
else if(Input.GetKey (KeyCode.D))
{
wheel.transform.Rotate(Vector3.up * Time.deltaTime * turnSpeed);
rotations += turnSpeed;
}
...
else //rotate to default
{
if (rotations < -1)
{
wheel.transform.Rotate(Vector3.up * Time.deltaTime * turnSpeed);
rotations += turnSpeed;
}
else if (rotations > 1)
{
wheel.transform.Rotate(-Vector3.up * Time.deltaTime * turnSpeed);
rotations -= turnSpeed;
}
else if (rotations != 0)
{
wheel.transform.localRotation = defaultRotation;
rotations = 0;
}
}
}
I think this should do it. There may be some inaccuracy with the floats so you can compare the rotations to 1 and -1 and you can just set the wheel to a default rotation. If you care about that 1 degree you need to do different checks though .
Note: if you don't really need to use the wheel's gameobject anywhere, store it's Transform instead. (Or if you need it in just a few cases you can still access it as wheel.gameobject). This would be easier on the processor as it doesn't need to access the transform every frame.
I tried it and it works but there's an issue where the position that the wheel resets itself to is slightly less than 0 degrees.
Are you sure this is not the initial rotation that is being set to defaultRotation? Do you somehow prohibit turning the wheel further than a certain rotation?
I see your y rotation is not set to 0. Are you sure the image isnt rotated or slightly tilted ?
I know this is a old thread but why isn't this working? everything functions but when I turn the car the position the steering wheel returns changes, any idea why?
Answer by saadali211 · Oct 03, 2018 at 08:29 AM
How to make a functional car steering wheel for a parking game. #Unity 3D #car control for parking game with #steering wheel with free script. Realistic car control with steering wheel UI to control car in unity game.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Wheels that just plain won't spin 0 Answers
WheelCollider rotates mesh wrong 1 Answer
Multiple Cars not working 1 Answer
Function Requires Two Button Presses Instead of One? 1 Answer