I have an on screen steering wheel that follows the mouse position.
How would I make it so, say, you click the upper left part of the wheel and drag down, it rotates the wheel left? And if you click the upper-right part and drag down it does the opposite? Right now I’m using this,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SteeringWheel : MonoBehaviour
{
public float wheelRotation;
Quaternion rotateGoal;
[SerializeField] float rotSpeed =1;
bool mouseClicked;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
if(mouseClicked)RotateWheel();
if(transform.rotation != rotateGoal)
{
transform.rotation = Quaternion.Lerp(transform.rotation, rotateGoal, rotSpeed * Time.deltaTime);
}
}
void RotateWheel()
{
Vector3 directionToMouse = (Input.mousePosition - transform.position).normalized;
float angle = 90 - Mathf.Atan2(directionToMouse.y, directionToMouse.x) * Mathf.Rad2Deg;
Vector3 rot = -transform.forward * angle;
rotateGoal = Quaternion.Euler(rot);
wheelRotation = -angle / 90f;
}
public void ClickMouse()
{
mouseClicked = true;
}
public void UnClickMouse()
{
mouseClicked = false;
rotateGoal = Quaternion.identity;
wheelRotation = 0;
}
}
The issue with the above script is if I rotate it 90 to the left, it will instantly go from -90 to 270, turning the number into -3 instead of 1. This number is accessed by the movement script and rotates the vehicle if it is moving forward.
So, how would I just have the number consistently subtract as you rotate it left, and vice versa if you rotate it right? I want to stop the number at a good 2 or 3, which should be easy, but I’m a little hazy on how to increase/decrease the value based on how much you’ve rotated it. I figure I can use Mathf.DeltaAngle? But won’t that still have the same issue with it randomly jumping after rotating 90 to the left?
Input.mousePosition is screen space coordinates
transform.position is world space coordinates
since you’re calculating in 2D, you should convert the world space transform.position to screen space, see the doc for Camera.WorldToScreenPoint Unity - Scripting API: Camera.WorldToScreenPoint .
havent checked the formula in detailled but that part is mandatory and should at least get your on the right track !
Hmm, it’s working fine, it’s a canvas element, it’s rotating towards the mouse just fine.
Sorry, maybe my code’s a little confusing, or maybe I’m just misunderstanding you, the “wheelRotation” is the value that is being used to get the actual input of the wheel(which is being accessed by another script), the 4 lines before, are being used to set the physical rotation of the steering wheel, which is working fine.
But the rotation isn’t the important part, the “wheelRotation” value is, I just want it to increase as it rotates to the right and decrease as it rotation to the left, this value is accessed by the movement script and turns the vehicle.
Bump, still trying to figure this out. Tried using DeltaAngle and Vector3.Dot and they both had issues. All I’m trying to do is make the wheelRotation subtract as it rotates left, and add as it rotates right. Does anyone know how to do this? I’m not very good with euler angles, it confuses me how they go from -90 to 270 and how I’m supposed to use this for math lol.
I would like the wheel to rotate as much as you want, and have the number stay, so logically it would get the rotation upon clicking the wheel, and calculate how much the wheel has rotated in relation to that rotation, I just don’t know how to do this.