Skip to content

Instantly share code, notes, and snippets.

@victorbstan
Created February 11, 2015 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save victorbstan/4dde0d0b4203c248423e to your computer and use it in GitHub Desktop.
Save victorbstan/4dde0d0b4203c248423e to your computer and use it in GitHub Desktop.
C# Unity suspension example script for wheel meshes to follow wheel collider suspensions
using UnityEngine;
using System.Collections;
// ADD THIS SCRIPT TO EACH OF THE WHEEL MESHES / WHEEL MESH CONTAINER OBJECTS
public class Wheel : MonoBehaviour {
public WheelCollider wheelC;
private Vector3 wheelCCenter;
private RaycastHit hit;
// Initialization
void Start () {
}
// Display
void Update () {
wheelCCenter = wheelC.transform.TransformPoint(wheelC.center);
if ( Physics.Raycast(wheelCCenter, -wheelC.transform.up, out hit, wheelC.suspensionDistance + wheelC.radius) ) {
transform.position = hit.point + (wheelC.transform.up * wheelC.radius);
} else {
transform.position = wheelCCenter - (wheelC.transform.up * wheelC.suspensionDistance);
}
}
// Physics
void FixedUpdate() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment