Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Wheel : MonoBehaviour {
- float spinAngle = 0;
- // Update is called once per frame
- void Update () {
- // assume the wheel art (the hub) is the only child
- Transform hub = transform.GetChild(0);
- // get wheel collider reference as correct type:
- WheelCollider wheel = collider as WheelCollider;
- // *6 converts RPM to Degrees per second (i.e. *360 and /60 )
- spinAngle += wheel.rpm * 6 * Time.deltaTime;
- // rotate hub according to spin and steering
- hub.localRotation = Quaternion.Euler( spinAngle, wheel.steerAngle, 0 );
- // use a raycast to detect current spring position
- RaycastHit hit;
- if ( Physics.Raycast ( transform.position, -transform.up, out hit, wheel.suspensionDistance + wheel.radius ) ) {
- // wheel is touching the ground
- float springPos = -(hit.distance-wheel.radius);
- hub.localPosition = Vector3.up * springPos;
- } else {
- // wheel is off the ground, use full spring distance
- hub.localPosition = -Vector3.up * wheel.suspensionDistance;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment