Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- public class JumpPad : MonoBehaviour {
- public Transform target;
- Transform thisTransform;
- public float extraHeight;
- public LayerMask launchableLayers;
- private Vector3 parabolaVertex;
- public Vector3 launchVector;
- public bool cacheLaunchVector;
- private Vector3 launchPosition;
- private Vector3 targetPosition;
- private float flightTime;
- private float curveHash;
- void Start ()
- {
- thisTransform = this.transform;
- targetPosition = target.position;
- launchPosition = this.transform.position;
- if (cacheLaunchVector)
- {
- launchVector = FindInitialVelocity(launchPosition, targetPosition, extraHeight);
- }
- }
- void OnTriggerEnter(Collider launchBody)
- {
- if (launchableLayers != (launchableLayers | (1 << launchBody.gameObject.layer)))
- {
- return;
- }
- if (cacheLaunchVector)
- {
- if (targetPosition != target.position || launchPosition != thisTransform.position)
- launchVector = FindInitialVelocity(launchPosition, targetPosition, extraHeight);
- launchBody.GetComponent<Rigidbody>().velocity = launchVector;
- }
- else
- {
- launchBody.GetComponent<Rigidbody>().velocity = FindInitialVelocity(launchBody.transform.position, targetPosition, extraHeight);
- }
- }
- [ContextMenu("Calculate Launch Vector")]
- private Vector3 FindInitialVelocity(Vector3 startPosition, Vector3 finalPosition, float maxHeightOffset)
- {
- //print("CALCULATING MOTHER FUCKER!");
- // get our return value ready. Default to (0f, 0f, 0f)
- Vector3 newVel = new Vector3();
- // Find the direction vector without the y-component
- Vector3 flatDirection = new Vector3(finalPosition.x, 0f, finalPosition.z) - new Vector3(startPosition.x, 0f, startPosition.z);
- // Find the distance between the two points (without the y-component)
- float range = flatDirection.magnitude;
- // Find unit direction of motion without the y component
- Vector3 unitDirection = flatDirection.normalized;
- // Find the max height
- float maxYPos = finalPosition.y + maxHeightOffset;
- // float maxYPos = startPosition.y + maxHeightOffset;
- if (startPosition.y > finalPosition.y) maxYPos = startPosition.y + maxHeightOffset;
- //maxYPos = parabolaVertexFloat;
- // find the initial velocity in y direction
- newVel.y = Mathf.Sqrt(-2.0f * Physics.gravity.y * (maxYPos - startPosition.y));
- // find the total time by adding up the parts of the trajectory
- // time to reach the max
- float timeToMax = Mathf.Sqrt(-2.0f * (maxYPos - startPosition.y) / Physics.gravity.y);
- // time to return to y-target
- float timeToTargetY = Mathf.Sqrt(-2.0f * (maxYPos - finalPosition.y) / Physics.gravity.y);
- // add them up to find the total flight time
- float totalFlightTime = timeToMax + timeToTargetY;
- flightTime = totalFlightTime;
- // find the magnitude of the initial velocity in the xz direction
- float horizontalVelocityMagnitude = range / totalFlightTime;
- // use the unit direction to find the x and z components of initial velocity
- newVel.x = horizontalVelocityMagnitude * unitDirection.x;
- newVel.z = horizontalVelocityMagnitude * unitDirection.z;
- return newVel;
- }
- #if UNITY_EDITOR
- void OnDrawGizmos()
- {
- targetPosition = target.position;
- launchPosition = this.transform.position;
- Vector3 directionBetween = targetPosition - launchPosition;
- if (!Mathf.Approximately(curveHash,targetPosition.y - launchPosition.y + extraHeight)) //if one of the positions has changed, recalculate the launch vector.
- {
- launchVector = FindInitialVelocity(launchPosition, targetPosition, extraHeight);
- }
- curveHash = targetPosition.y - launchPosition.y + extraHeight;
- Vector3 a = this.transform.position;
- Vector3 b = target.position;
- Gizmos.color = Color.grey;
- Gizmos.DrawLine(a, b);
- float count = 20;
- Vector3 lastPoint = a;
- for (float i = 0; i < count + 1; i++)
- {
- float percent = i / count;
- float midFlightTime = (percent * flightTime);
- float yHeight = launchVector.y * midFlightTime - 0.5f * -Physics.gravity.y * midFlightTime * midFlightTime;
- Vector3 point = launchPosition + directionBetween * percent;
- point.y = yHeight+launchPosition.y;
- Gizmos.color = Color.green;
- Gizmos.DrawLine(lastPoint, point);
- lastPoint = point;
- }
- }
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment