Thanks alot ![]()
I kinda fixed it, or atleast it drives ok now and this is how the suspension looks while driving:
Iam honestly not really sure why it works now. I switched back to the approach with the accelerationPoint and change the CG as @CodeSmile recommend in the settings of the Rigidbody. Before that I changed the values in my script before. I also noticed that my AddForceAtPosition points for my lateral grip where wrong and maybe that also messed with the weight transfer, but I honestly don’t know. Thats my changes:
// Throttle
private void ApplyThrottle(Transform tireTransformFront, Transform tireTransformRear, bool v)
{
RaycastHit hit;
Vector3 frontRayOrigin = tireTransformFront.position;
Vector3 rearRayOrigin = tireTransformRear.position;
Vector3 frontRayDir = -tireTransformFront.up;
Vector3 rearRayDir = -tireTransformRear.up;
RaycastHit frontHit;
RaycastHit rearHit;
bool frontRayDidHit = Physics.Raycast(frontRayOrigin, frontRayDir, out frontHit, frontRaycastLength, groundLayer);
bool rearRayDidHit = Physics.Raycast(rearRayOrigin, rearRayDir, out rearHit, rearRaycastLength, groundLayer);
bool rayDidHit = frontRayDidHit || rearRayDidHit;
if (rayDidHit)
{
float throttleInput = Mathf.Max(0f, thbrInput);
Vector3 forwardDir = carTransform.forward;
float carForwardSpeed = Vector3.Dot(forwardDir, carRigidBody.linearVelocity);
carVelocityRatio = Mathf.Clamp01(Mathf.Abs(carForwardSpeed) / maxSpeed);
float motorTorqueCurve = powerCurve.Evaluate(carVelocityRatio);
float throttle = (throttleInput * motorTorque * motorTorqueCurve);
//carRigidBody.AddForce(carTransform.forward * throttle, ForceMode.Force);
//carRigidBody.AddForceAtPosition(carTransform.forward * throttle, frontAccelerationPoint.position, ForceMode.Force);
carRigidBody.AddForceAtPosition(carTransform.forward * throttle, rearAccelerationPoint.position, ForceMode.Force);
}
}
// Brake / Drag Brake
private void ApplyBrake(Transform tireTransformFront, Transform tireTransformRear, bool v)
{
RaycastHit hit;
Vector3 frontRayOrigin = tireTransformFront.position;
Vector3 rearRayOrigin = tireTransformRear.position;
Vector3 frontRayDir = -tireTransformFront.up;
Vector3 rearRayDir = -tireTransformRear.up;
RaycastHit frontHit;
RaycastHit rearHit;
bool frontrayDidHit = Physics.Raycast(frontRayOrigin, frontRayDir, out frontHit, frontRaycastLength, groundLayer);
bool rearrayDidHit = Physics.Raycast(rearRayOrigin, rearRayDir, out rearHit, rearRaycastLength, groundLayer);
bool rayDidHit = frontrayDidHit || rearrayDidHit;
float forwardSpeed = Vector3.Dot(carRigidBody.linearVelocity, carTransform.forward);
if (rayDidHit)
{
if (forwardSpeed > 0f)
{
float brakeInput = MathF.Abs(Mathf.Min(0f, thbrInput));
Vector3 brakeVec = -carTransform.forward;
carVelocityRatio = Mathf.Clamp01(Mathf.Abs(forwardSpeed) / maxSpeed);
float brakeForceCurve = brakeCurve.Evaluate(carVelocityRatio);
float brake = (brakeInput * brakeForce * brakeForceCurve);
//carRigidBody.AddForce(-carTransform.forward * brake, ForceMode.Force);
//carRigidBody.AddForceAtPosition(-carTransform.forward * brake, frontAccelerationPoint.position, ForceMode.Force);
carRigidBody.AddForceAtPosition(-carTransform.forward * brake, rearAccelerationPoint.position, ForceMode.Force);
// Drag Brake
if (Mathf.Abs(thbrInput) < 0.05f && rayDidHit)
{
//carRigidBody.AddForce(-carTransform.forward * dragBrake, ForceMode.Force);
//carRigidBody.AddForceAtPosition(-carTransform.forward * dragBrake, frontAccelerationPoint.position, ForceMode.Force);
carRigidBody.AddForceAtPosition(-carTransform.forward * dragBrake, rearAccelerationPoint.position, ForceMode.Force);
}
}
}
}
I outsourced throttle and brake form the suspension function. I copied and pasted the rayDidHit stuff I used before and applied it to throttle and brake.
And my fixedUpdate:
for (int i = 0; i < tireTransformFront.Length; i++)
{
Transform front = tireTransformFront[i];
Transform rear = tireTransformRear[i];
ApplyThrottle(front, rear, true);
}
for (int i = 0; i < tireTransformFront.Length; i++)
{
Transform front = tireTransformFront[i];
Transform rear = tireTransformRear[i];
ApplyBrake(front, rear, true);
}
ChatGPT came up with this and I dont really understand why I needed to write the code like this and couldnt just call the function.
Thats a good idea to do that with a force. I tried to do that with changing grip levels:
float frontGripDownforceMultiplier = frontDownforceCurve.Evaluate(carVelocityRatio);
float frontGripRatio = (frontGrip * frontGripMultiplier) + (frontDownforceCoefficient * frontGripDownforceMultiplier);
Thanks! I will try that too.