Advertisement
Cookie042

Spring Hovercar

Feb 7th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.36 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class HoverCar : MonoBehaviour
  7. {
  8.  
  9.     public float movespeed;
  10.     public float rotationSpeed;
  11.  
  12.     public Vector3[] hoverPoints;
  13.  
  14.     public Spring spring = new Spring(1f, .2f, .2f, Spring.SpringForceMode.PUSH);
  15.  
  16.     public float hoverHeight;
  17.     //public float hoverStiffness;
  18.     //public float hoverDamping;
  19.  
  20.     public LayerMask hitMask;
  21.  
  22.     private Rigidbody rb;
  23.  
  24.     // Start is called before the first frame update
  25.     void OnEnable()
  26.     {
  27.         rb = GetComponent<Rigidbody>();
  28.         //spring = new Spring(hoverHeight, hoverDamping, hoverStiffness, Spring.SpringForceMode.PUSH);
  29.     }
  30.  
  31.     private void Update()
  32.     {
  33.         if (Input.GetKey(KeyCode.UpArrow))
  34.         {
  35.             rb.AddForce(transform.forward * movespeed * Time.deltaTime);
  36.         }
  37.         if (Input.GetKey(KeyCode.DownArrow))
  38.         {
  39.             rb.AddForce(-transform.forward * movespeed * Time.deltaTime);
  40.         }
  41.         if (Input.GetKey(KeyCode.LeftArrow))
  42.         {
  43.             rb.AddTorque(-transform.up * rotationSpeed * Time.deltaTime);
  44.         }
  45.         if (Input.GetKey(KeyCode.RightArrow))
  46.         {
  47.             rb.AddTorque(transform.up * rotationSpeed * Time.deltaTime);
  48.         }
  49.     }
  50.  
  51.     private void OnValidate()
  52.     {
  53.         spring.length = hoverHeight;
  54.         //spring.stiffnessCoef = hoverStiffness;
  55.         //spring.dampingCoef = hoverDamping;
  56.     }
  57.  
  58.     // Update is called once per frame
  59.     void FixedUpdate()
  60.     {
  61.         var mtx = transform.localToWorldMatrix;
  62.  
  63.         foreach (var hoverPoint in hoverPoints)
  64.         {
  65.             var pos = mtx.MultiplyPoint(hoverPoint);
  66.  
  67.             //var subMass = rb.mass / hoverPoints.Length;
  68.  
  69.             if (Physics.Raycast(pos, Vector3.down, out RaycastHit hit, hitMask ))
  70.             {
  71.  
  72.                 var worldDelta = pos - transform.position;
  73.                 var rvel = Vector3.Cross(-worldDelta, rb.angularVelocity) + rb.velocity;
  74.  
  75.                 //Debug.DrawRay(pos, rvel);
  76.                 //Debug.DrawLine(pos, hit.point);
  77.  
  78.                 var force = spring.GetSpringForce(pos, hit.point, rvel, Vector3.zero) * Time.deltaTime;
  79.  
  80.                 rb.AddForceAtPosition(force / hoverPoints.Length, pos, ForceMode.Force);
  81.                 Debug.DrawRay(pos, force / 20f, new Color(1f, 0.92f, 0.02f, 0.25f));
  82.             }
  83.         }
  84.     }
  85.  
  86.     private void OnDrawGizmos()
  87.     {
  88.         Gizmos.color = Color.red;
  89.  
  90.         for (int i = 0; i < hoverPoints.Length; i++)
  91.         {
  92.             var worldPos = transform.localToWorldMatrix.MultiplyPoint(hoverPoints[i]);
  93.  
  94.             Gizmos.DrawWireCube(worldPos, Vector3.one * .01f);
  95.         }
  96.     }
  97. }
  98.  
  99.  
  100. /////////SPRING CLASS
  101. using UnityEngine;
  102.  
  103. [System.Serializable]
  104. public class Spring {
  105.  
  106.     public float length = 1;
  107.  
  108.     public float dampingCoef = .5f;
  109.     public float stiffnessCoef = .5f;
  110.  
  111.     public SpringForceMode forceMode;
  112.  
  113.     public Spring(float length, float dampingCoef, float stiffnessCoef, SpringForceMode forceMode)
  114.     {
  115.         this.length = length;
  116.         this.dampingCoef = dampingCoef;
  117.         this.stiffnessCoef = stiffnessCoef;
  118.         this.forceMode = forceMode;
  119.     }
  120.  
  121.     [System.Serializable]
  122.     public enum SpringForceMode
  123.     {
  124.         PUSH,
  125.         PULL,
  126.         BOTH
  127.     }
  128.  
  129.     public Vector3 GetSpringForce(Vector3 endPosition1, Vector3 endPosition2, Vector3 endVelocity1, Vector3 endVelocity2)
  130.     {
  131.         var delta = endPosition1 - endPosition2;
  132.  
  133.         var displacement = delta - (delta.normalized * length);
  134.         var radialVel1 = (endVelocity1.magnitude * Mathf.Cos(Vector3.Angle(delta, endVelocity1) * Mathf.Deg2Rad)) * delta.normalized;
  135.         var radialVel2 = (endVelocity2.magnitude * Mathf.Cos(Vector3.Angle(delta, endVelocity2) * Mathf.Deg2Rad)) * delta.normalized;
  136.  
  137.         //may need to remove this...this is meant to do damping based on a scaled value;
  138.         var stiffness = Time.deltaTime * stiffnessCoef;
  139.         var damping = Time.deltaTime * dampingCoef;
  140.  
  141.         //springForce = −m/Δt^2 * Ck * x − m/Δt Cd * v
  142.         //var springForce = -stiffness * displacement - damping * (radialVel1 - radialVel2);
  143.  
  144.         //springForce = −m/Δt^2 * Ck * x − m/Δt Cd * v
  145.         var springForce = -stiffnessCoef * displacement - dampingCoef * (radialVel1 - radialVel2);
  146.  
  147.         switch (forceMode)
  148.         {
  149.             case SpringForceMode.PUSH:
  150.                 if (delta.magnitude > length) springForce = Vector3.zero;
  151.                 break;
  152.             case SpringForceMode.PULL:
  153.                 if (delta.magnitude < length) springForce = Vector3.zero;
  154.                 break;
  155.             case SpringForceMode.BOTH: break;
  156.             default:
  157.                 throw new System.ArgumentOutOfRangeException();
  158.         }
  159.         return springForce;
  160.     }
  161.  
  162.     public void DrawGizmo(Vector3 p1, Vector3 p2, float blendRange = 1)
  163.     {
  164.         float d = Vector3.Distance(p1, p2);
  165.         if (d > length)
  166.             Gizmos.color = Color.Lerp(Color.white, Color.red, Mathf.Clamp(d - length, 0f, blendRange));
  167.         else if (d < length)
  168.             Gizmos.color = Color.Lerp(Color.white, Color.blue, Mathf.Clamp(Mathf.Abs(d - length), 0f, blendRange));
  169.         else
  170.             Gizmos.color = Color.white;
  171.  
  172.         Gizmos.DrawLine(p1, p2);
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement