I'm working on a car using WheelColliders. How do I script the WheelColliders so that at high speeds, the SteerAngle decreases in proportion to how fast the car is traveling?
3 Answers
3Try this:
SteerAngle = Input.GetAxis("Horizontal")*1/rigidBody.velocity
Bullshit!
rigidbody.velocity is Vector3, Axis input is a float (from 0 to 1) - this can’t work!
try with rigidbody.velocity.magnitude, but not like shown above
something like
float maxSteerAngle = 40;
float rv = rigidbody.velocity.magnitude;
if(rv < 10) {
rv = 1;
}
myWheelCollider.steerAngle = Input.GetAxis("Horizontal") * maxSteerAngle * 1 / rv;
but I’ve not tested this - so don’t blame me ![]()
Hey, this is from ages ago and I didn’t even end up using what you suggested but I thought id share my solution. Basically the following makes the steering slower when you get closer to the max amount, allowing easy small turns as well as well planned larger turns.
float anglePct = 100 / (m_MaximumSteerAngle / m_SteerAngle);
float minWaitTime = 5;
if (m_SteerAngle == 0)
{
anglePct = 100 / m_MaximumSteerAngle;
print("");
}
anglePct = Mathf.Abs(anglePct); //Make our number positive if its negaitve
anglePct = Mathf.Clamp(anglePct, minWaitTime, m_MaximumSteerAngle); //Clamp between min and max to reduce initial wait time and avoid extremities
m_SteerAngle = (m_SteerAngle + (steering * Time.deltaTime * anglePct * steerIntensity));
m_SteerAngle = Mathf.Clamp(m_SteerAngle, -m_MaximumSteerAngle, m_MaximumSteerAngle);
The values that arent made either in that code or within the Unity Standard Assets Car Controller.cs are:
- steerIntensity - This is a percent value that either increases or decreases the overall time taken to turn. (1 = ‘normal’)
I also found that unless you drastically increase the max steer angle or mess with these numbers a lot, it can be quite difficult to steer when doing a U-Turn or something else slow. to fix this i did the following which
float slowMag = m_Rigidbody.velocity.magnitude;
if (slowMag < 10)
{
slowMag = slowSteeringEffector;
}
else
{
slowMag = 1;
}
To implement this just replace:
m_SteerAngle = Mathf.Clamp(m_SteerAngle, -m_MaximumSteerAngle, m_MaximumSteerAngle);
with:
m_SteerAngle = Mathf.Clamp(m_SteerAngle, -m_MaximumSteerAngle * slowMag, m_MaximumSteerAngle * slowMag);