Jon Rurka - 6 months ago 50

C# Question

This piece of code is supposed to draw a line representing the turning radius of an object using it's velocity and angular velocity. For some reason, though, the radius given is way longer that it's supposed to be:

`AngularVel = m_Rigidbody.angularVelocity.y;`

Vel = m_Rigidbody.velocity.magnitude;

//Vel = transform.InverseTransformDirection(m_Rigidbody.velocity).z; same result

float radius = Vel / (AngularVel * Mathf.Deg2Rad);

Quaternion RadiusDirQuat = Quaternion.AngleAxis(m_Rigidbody.rotation.eulerAngles.y + 90, Vector3.up);

Vector3 radiusDir = RadiusDirQuat * Vector3.forward;

Debug.DrawRay(m_Rigidbody.position + Vector3.up, radiusDir * radius, new Color(1, 0, 0.5f, 1));

I'm using this article for reference: https://revisionmaths.com/advanced-level-maths-revision/mechanics/motion-circle

Answer

According to this, Unity's angular velocity is already in radians per second, yet you are multiplying by `Mathf.Deg2Rad`

, so just remove that and make the line read: `float radius = Vel / AngularVel`

.