Advertisement
Purianite

Untitled

Mar 25th, 2019
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.98 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class RaycastWheelSimple : MonoBehaviour {
  5.  
  6.     // Simple Vehicle Raycast wheel
  7.  
  8.     public Transform graphic;
  9.  
  10.     public float mass = 1.0f;
  11.     public float radius = 1.0f;
  12.     public float maxSuspension = 0.2f;
  13.     public float spring = 100.0f;
  14.     public float damper = 0.0f;
  15.  
  16.     public float wheelAngle = 0f;
  17.  
  18.     private float circumference;
  19.  
  20.     private float contactPatchArea;
  21.     private Rigidbody parent;
  22.     private bool grounded = false;
  23.  
  24.     public bool IsGrounded
  25.     {
  26.         get
  27.         {
  28.             return grounded;
  29.         }
  30.     }
  31.  
  32.     void  Awake ()
  33.     {
  34.         circumference = (radius * 2) * Mathf.PI;
  35.         parent = transform.root.GetComponent<Rigidbody>();
  36.     }
  37.  
  38.     void  FixedUpdate ()
  39.     {
  40.         GetGround();
  41.     }
  42.  
  43.     void  GetGround (){
  44.         grounded = false;
  45.         Vector3 downwards = transform.TransformDirection (-Vector3.up);
  46.         RaycastHit hit;
  47.  
  48.         // down = local downwards direction
  49.         Vector3 down = transform.TransformDirection(Vector3.down);
  50.  
  51.         if (Physics.Raycast(transform.position, downwards, out hit, radius + maxSuspension)) {
  52.  
  53.             grounded = true;
  54.             // the velocity at point of contact
  55.             Vector3 velocityAtTouch = parent.GetPointVelocity(hit.point);
  56.  
  57.             // calculate spring compression
  58.             // difference in positions divided by total suspension range
  59.             float compression = hit.distance / (maxSuspension + radius);
  60.             compression = -compression + 1;
  61.  
  62.             // final force
  63.             Vector3 force = -downwards * compression * spring;
  64.             // velocity at point of contact transformed into local space
  65.  
  66.             Vector3 t = transform.InverseTransformDirection(velocityAtTouch);
  67.  
  68.             // local x and z directions = 0
  69.             t.z = t.x = 0;
  70.  
  71.             // back to world space * -damping
  72.             Vector3 damping = transform.TransformDirection(t) * -damper;
  73.             Vector3 finalForce = force + damping;
  74.  
  75.             // VERY simple turning - force rigidbody in direction of wheel
  76.             t = parent.transform.InverseTransformDirection(velocityAtTouch);
  77.             t.y = 0;
  78.             t.z = 0;
  79.  
  80.             t = transform.TransformDirection(t);
  81.  
  82.             parent.AddForceAtPosition(finalForce + (t), hit.point);
  83.    
  84.             if (graphic) graphic.position = transform.position + (down * (hit.distance - radius));
  85.  
  86.         }
  87.         else
  88.         {
  89.             if (graphic) graphic.position = transform.position + (down * maxSuspension);
  90.         }
  91.            
  92.         float speed = parent.velocity.magnitude;
  93.  
  94.         if (graphic)
  95.         {
  96.             //graphic.transform.localEulerAngles = new Vector3 (graphic.transform.localEulerAngles.x, wheelAngle, graphic.transform.localEulerAngles.z);
  97.             //graphic.transform.Rotate (360 * (speed / circumference) * Time.fixedDeltaTime, 0, 0);
  98.         }
  99.     }
  100.  
  101.     void  OnDrawGizmosSelected ()
  102.     {
  103.         Gizmos.color = new Color(0,1,0,1);
  104.         Vector3 direction = transform.TransformDirection (-Vector3.up) * (this.radius);
  105.         Gizmos.DrawRay(transform.position,direction);
  106.  
  107.         Gizmos.color = new Color(0,0,1,1);
  108.         direction = transform.TransformDirection (-Vector3.up) * (this.maxSuspension);
  109.         Gizmos.DrawRay(new Vector3(transform.position.x,transform.position.y - radius,transform.position.z),direction);
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement