- Home /
Question by
vulgarknight · May 19, 2016 at 04:56 AM ·
physicscarwheel
Car physics problem
Working on some basic suspension physics and I've run into an issue in which my car floats when in contact with the ground.
Here's my code, anyone have any insight?
using UnityEngine;
using System.Collections;
public class suspensionPhys : MonoBehaviour {
public Rigidbody rb;
public GameObject carObj;
public carPhys carScript;
[Header("Suspension")]
public float springForce;
public float damperForce;
public float springConstant;
public float damperConstant;
public float restLength;
private float previousLength;
private float currentLength;
private float springVelocity;
// Use this for initialization
void Start () {
rb = carObj.transform.GetComponent<Rigidbody>();
carScript = carObj.GetComponent<carPhys>();
}
// Update is called once per frame
void FixedUpdate () {
RaycastHit hit;
if (Physics.Raycast(transform.position, -transform.up, out hit, restLength + carScript.wheelRadius)) {
previousLength = currentLength;
currentLength = restLength - (hit.distance - carScript.wheelRadius);
springVelocity = (currentLength - previousLength) / Time.fixedDeltaTime;
springForce = springConstant * currentLength;
damperForce = damperConstant * springVelocity;
rb.AddForceAtPosition(transform.up * (springForce + damperForce), transform.position);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Strange behaviour when WheelColliders are on their side. 0 Answers
2D Joint/Wheel Question 1 Answer
in Unity 3.5.7 car starts to shake 0 Answers
Square wheels 4 Answers