Edwarddv

Jump Pad

Mar 21st, 2016
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.93 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6.  
  7. public class JumpPad : MonoBehaviour {
  8.  
  9.     public Transform target;
  10.     Transform thisTransform;
  11.  
  12.     public float extraHeight;
  13.     public LayerMask launchableLayers;
  14.     private Vector3 parabolaVertex;
  15.     public Vector3 launchVector;
  16.     public bool cacheLaunchVector;
  17.     private Vector3 launchPosition;
  18.     private Vector3 targetPosition;
  19.     private float flightTime;
  20.     private float curveHash;
  21.  
  22.  
  23.  
  24.     void Start ()
  25.     {
  26.         thisTransform = this.transform;
  27.         targetPosition = target.position;
  28.         launchPosition = this.transform.position;
  29.  
  30.         if (cacheLaunchVector)
  31.         {
  32.             launchVector = FindInitialVelocity(launchPosition, targetPosition, extraHeight);
  33.         }
  34.     }
  35.  
  36.  
  37.     void OnTriggerEnter(Collider launchBody)
  38.     {
  39.         if (launchableLayers != (launchableLayers | (1 << launchBody.gameObject.layer)))
  40.         {
  41.             return;
  42.  
  43.         }
  44.         if (cacheLaunchVector)
  45.         {
  46.             if (targetPosition != target.position || launchPosition != thisTransform.position)
  47.                 launchVector = FindInitialVelocity(launchPosition, targetPosition, extraHeight);
  48.  
  49.  
  50.             launchBody.GetComponent<Rigidbody>().velocity = launchVector;
  51.         }
  52.         else
  53.         {
  54.        
  55.             launchBody.GetComponent<Rigidbody>().velocity = FindInitialVelocity(launchBody.transform.position, targetPosition, extraHeight);
  56.  
  57.         }
  58.  
  59.     }
  60.  
  61.  
  62.     [ContextMenu("Calculate Launch Vector")]
  63.     private Vector3 FindInitialVelocity(Vector3 startPosition, Vector3 finalPosition, float maxHeightOffset)
  64.     {
  65.         //print("CALCULATING MOTHER FUCKER!");
  66.         // get our return value ready. Default to (0f, 0f, 0f)
  67.         Vector3 newVel = new Vector3();
  68.         // Find the direction vector without the y-component
  69.         Vector3 flatDirection = new Vector3(finalPosition.x, 0f, finalPosition.z) - new Vector3(startPosition.x, 0f, startPosition.z);
  70.         // Find the distance between the two points (without the y-component)
  71.         float range = flatDirection.magnitude;
  72.  
  73.         // Find unit direction of motion without the y component
  74.         Vector3 unitDirection = flatDirection.normalized;
  75.         // Find the max height
  76.        
  77.         float maxYPos = finalPosition.y + maxHeightOffset;
  78.  
  79.        // float maxYPos = startPosition.y + maxHeightOffset;
  80.         if (startPosition.y > finalPosition.y) maxYPos = startPosition.y + maxHeightOffset;
  81.  
  82.         //maxYPos = parabolaVertexFloat;
  83.  
  84.  
  85.         // find the initial velocity in y direction
  86.         newVel.y = Mathf.Sqrt(-2.0f * Physics.gravity.y * (maxYPos - startPosition.y));
  87.         // find the total time by adding up the parts of the trajectory
  88.         // time to reach the max
  89.         float timeToMax = Mathf.Sqrt(-2.0f * (maxYPos - startPosition.y) / Physics.gravity.y);
  90.         // time to return to y-target
  91.         float timeToTargetY = Mathf.Sqrt(-2.0f * (maxYPos - finalPosition.y) / Physics.gravity.y);
  92.         // add them up to find the total flight time
  93.         float totalFlightTime = timeToMax + timeToTargetY;
  94.         flightTime = totalFlightTime;
  95.         // find the magnitude of the initial velocity in the xz direction
  96.         float horizontalVelocityMagnitude = range / totalFlightTime;
  97.         // use the unit direction to find the x and z components of initial velocity
  98.         newVel.x = horizontalVelocityMagnitude * unitDirection.x;
  99.         newVel.z = horizontalVelocityMagnitude * unitDirection.z;
  100.  
  101.  
  102.         return newVel;
  103.     }
  104.  
  105. #if UNITY_EDITOR
  106.     void OnDrawGizmos()
  107.     {
  108.    
  109.         targetPosition = target.position;
  110.         launchPosition = this.transform.position;
  111.         Vector3 directionBetween = targetPosition - launchPosition;
  112.  
  113.  
  114.         if (!Mathf.Approximately(curveHash,targetPosition.y - launchPosition.y + extraHeight)) //if one of the positions has changed, recalculate the launch vector.
  115.         {
  116.             launchVector = FindInitialVelocity(launchPosition, targetPosition, extraHeight);
  117.         }
  118.  
  119.         curveHash = targetPosition.y - launchPosition.y + extraHeight;
  120.  
  121.         Vector3 a = this.transform.position;
  122.         Vector3 b = target.position;
  123.  
  124.         Gizmos.color = Color.grey;
  125.  
  126.         Gizmos.DrawLine(a, b);
  127.  
  128.  
  129.         float count = 20;
  130.         Vector3 lastPoint = a;
  131.  
  132.         for (float i = 0; i < count + 1; i++)
  133.         {
  134.             float percent = i / count;
  135.             float midFlightTime = (percent * flightTime);
  136.             float yHeight = launchVector.y * midFlightTime - 0.5f * -Physics.gravity.y * midFlightTime * midFlightTime;
  137.  
  138.             Vector3 point = launchPosition + directionBetween * percent;
  139.             point.y = yHeight+launchPosition.y;
  140.             Gizmos.color = Color.green;
  141.  
  142.             Gizmos.DrawLine(lastPoint, point);
  143.  
  144.             lastPoint = point;
  145.  
  146.         }
  147.  
  148.  
  149.     }
  150. #endif
  151. }
Advertisement
Add Comment
Please, Sign In to add comment