Hi all
I’m very new to unity and have been playing with a prototype to try and get a car moving around in 3d. Ive gotten some suspension working based on a rigidbody with 4 raycasts for the wheels, but im getting in a muddle with the steering side of things
I have an Empty GameObject with a RigidBody component attached, lets call that CarPrototype. It contains a simple cuboid to represent the car itself. The CarPrototype has a script that handles input from controller
this.steering = maxSteeringAngle * (controls.Gameplay.SteerRight.ReadValue<float>() - controls.Gameplay.SteerLeft.ReadValue<float>());
wheelFR.transform.eulerAngles = new Vector3(0, this.steering, 0);
wheelFL.transform.eulerAngles = new Vector3(0, this.steering, 0);
The CarPrototype has 4 Empty GameObjects that i use for raycasting for suspension and am trying to use for steering. The specific steering part of the script attached to these “Wheel” objects is as follows
/**
* Steering
*
*/
// world space velocity of this tire...
Vector3 tireWorldVel = rb.GetPointVelocity(transform.position);
Vector3 steeringDir = transform.right;
Debug.Log(transform.position);
float steeringVel = Vector3.Dot(steeringDir, tireWorldVel);
Debug.Log("Steering Vel = " + Math.Round(steeringVel,2));
float desiredVelChange = - steeringVel * tyreGripFactor;
float desiredAccel = desiredVelChange / Time.fixedDeltaTime;
rb.AddForceAtPosition(steeringDir * tyreMass * desiredAccel, transform.position);
What seems to happen is that the car steers to the maxSteeringAngle in degrees from the z axis, but then wont steer any further. If i release the controller, the car automatically steers back to point along the z axis. Its almost like the velocity that im trying to add force against is not calculated correctly. Can anyone help point me in the right direction (literally
)
Thanks