Question by
JesseFox · Apr 01, 2016 at 12:25 AM ·
addforceaddforceatposition
Suspension Help
I have this script to create a car like suspension but the only problem is that it doesn't have a resting point like a real life Wheel suspension would have, is there anyway where i can stop it acting like a hover car type suspension and have it more like a car suspension? if anyone could help that would be amazing :)
using UnityEngine; using System.Collections;
public class testSuspension : MonoBehaviour {
public float springStrength;
public float springDistance;
public Transform[] Springs;
public Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate ()
{
RaycastHit hit;
foreach (Transform Spring in Springs)
{
Vector3 downwardForce;
float distancePercentage;
Debug.DrawRay(Spring.position, -Spring.up * springDistance , Color.green);
if (Physics.Raycast (Spring.position, Spring.up * -1, out hit, springDistance))
{
distancePercentage = 1 - (hit.distance / springDistance);
downwardForce = transform.up * springStrength * distancePercentage;
downwardForce = downwardForce * Time.deltaTime * rb.mass;
rb.AddForceAtPosition (downwardForce, Spring.position);
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Using Raycast, add force on the touched point of a gameobject 1 Answer
left force right force 0 Answers
Best ads for mobile game? 1 Answer