- Home /
Wheel rotation, what am I doing wrong?
Ok so I am trying to get a wheel to rotate an object as if it is a wheel of a known diameter and traveling a known distance, and I need to know how much to rotate the object.
My understanding is the equation for that is ( distance / circumference = degrees to rotate ), where circumference = diameter * pi. Except when I try to actually apply that equation the rotations I am getting are tiny compared to the movement.
public class WheelDebug : MonoBehaviour
{
public float MoveSpeed = 1;
public float Diamiter = 1.41f;
// Update is called once per frame
void Update()
{
float distanceMoved = MoveSpeed * Time.deltaTime;
transform.position += Vector3.forward * distanceMoved;
float circumference = Diamiter * Mathf.PI;
float rotationToTravelDistance = distanceMoved / circumference;
transform.rotation *= Quaternion.Euler(rotationToTravelDistance, 0, 0);
}
}
So based on my understanding, what is happening above is every frame, which occurs ~60 times a second, my object moves MoveSpeed * Time.deltaTime, resulting in the object moving 1 unity unit or meter, per second in game, regardless of framerate.
Since I record it as distanceMoved, I can then plug that into the wheel turn calculation since I know that is the exact distance the object moved this frame, and.... I know what I did wrong.
Posting this anyway so anyone running into the same problem
Answer by CiberX15 · Jul 27, 2021 at 01:18 AM
Ok so the problem was my equation was correctly returning the PERCENTAGE of how far I needed to turn, it wasn't returning the degrees of rotation. So all I had to do to make it work seamlessly is multiply the rotation travel distance by 360. So the corrected code is as follows:
public class WheelDebug : MonoBehaviour
{
public float MoveSpeed = 1;
public float Diamiter = 1.41f;
// Update is called once per frame
void Update()
{
float distanceMoved = MoveSpeed * Time.deltaTime;
transform.position += Vector3.forward * distanceMoved;
float circumference = Diamiter * Mathf.PI;
float rotationToTravelDistance = distanceMoved / circumference;
transform.rotation *= Quaternion.Euler(rotationToTravelDistance * 360, 0, 0);
}
}
glad you sorted it.
ps, 360 degrees in a circle, 365 days in a year.