- Home /
Steering Wheel with mouse drag
What I need is a steering wheel that rotates with the mouse as if the mouse was a hand on the wheel. I got it to work with a top down camera but I need the camera in its standard 0,0,0 rotation. btw the sphere is the mouse
Script:
public Transform body;
public string side;
public Vector3 worldPos;
public Vector3 startPos;
public float angleFromStart;
GameObject cam;
float mouseX;
float mouseY;
float cameraDif;
void Start()
{
startPos = transform.position;
cam = GameObject.FindGameObjectWithTag("MainCamera");
cameraDif = cam.transform.position.y - transform.position.y;
}
void Update()
{
mouseX = Input.mousePosition.x;
mouseY = Input.mousePosition.y;
worldPos = cam.camera.ScreenToWorldPoint(new Vector3(mouseX, mouseY, cameraDif));
body.position = worldPos;
if (Vector3.Angle(body.right, startPos - body.position) > 90f) side = "left"; else side = "right";
if (side == "right")
{
angleFromStart = Vector3.Angle(body.position - startPos, Vector3.forward) - 360;
angleFromStart = angleFromStart * -1;
}
else
{
angleFromStart = Vector3.Angle(body.position - startPos, Vector3.forward);
}
transform.eulerAngles = new Vector3(0, angleFromStart, 0);
Debug.Log(side);
}
capture.png
(93.5 kB)
Comment