I am currently creating a simple VR racing game and cannot get the wheel to rotate properly, the issue is shown here
wheelGrabOrigin is the position in the middle of the wheel.
WheelGrab is the cube that I am moving with the controller.
EDIT: I solved the issue, turns out I was using local position in InverseTransformPoint as pointed out by @StarManta which was throwing it off, I was also using Atan2 slightly off and was able to fix it using the code provided by @Kurt-Dekker . Thank You!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Wheel : MonoBehaviour
{
public Transform wheelGrab;
public Transform wheelGrabOrigin;
[SerializeField] private float angle;
void Update()
{
Vector3 reletive = wheelGrabOrigin.InverseTransformPoint(wheelGrab.localPosition);
angle = (Mathf.Atan2(Mathf.Abs(reletive.x), Mathf.Abs(reletive.y))) * Mathf.Rad2Deg;
transform.localRotation = Quaternion.Euler(new Vector3(angle, transform.localEulerAngles.y, transform.localEulerAngles.z));
}
}
By taking the Abs of the inputs to Mathf.Atan2() you are guaranteeing it will always return only one quarter of its possible output, so I don’t think you want that.
Enclosed is an ultra-simple package showing gripping a spinny thing and manipulating it with Atan2().
Thank you, I realized later that using Mathf.Abs() would cause some issues but it was better than without. For whatever reason without Mathf.Abs() the wheel was spinning uncontrollably. Ill try and implement the code in the package.
InverseTransformPoint expects world position, and you’re sending it local position. That’s going to create unpredictable behavior. Especially if wheelGrab is in the hierarchy of your wheel (and I have to assume that it is?), then rotating the wheel is going to make that local position and the world position entirely different.