Advertisement
Teonnyn

Untitled

Mar 22nd, 2017
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.13 KB | None | 0 0
  1.     // Find Velocity based on position calculation //
  2.     private Vector3 findInitialVelocity(Vector3 startPosition, Vector3 finalPosition, float maxHeightOffset = 1.6f, float rangeOffset = 0.11f)
  3.     {
  4.         Vector3 newVel = new Vector3();
  5.         Vector3 direction = new Vector3(finalPosition.x, 0f, finalPosition.z) - new Vector3(startPosition.x, 0f, startPosition.z);
  6.         float range = direction.magnitude;
  7.         range += rangeOffset;
  8.         Vector3 unitDirection = direction.normalized;
  9.         float maxYPos = finalPosition.y + maxHeightOffset;
  10.         newVel.y = Mathf.Sqrt(-2.0f * Physics.gravity.y * (maxYPos - startPosition.y));
  11.         float timeToMax = Mathf.Sqrt(-2.0f * (maxYPos - startPosition.y) / Physics.gravity.y);
  12.         float timeToTargetY = Mathf.Sqrt(-2.0f * (maxYPos - finalPosition.y) / Physics.gravity.y);
  13.         float totalFlightTime = (timeToMax / 10) + timeToTargetY;
  14.         float horizontalVelocityMagnitude = range / totalFlightTime;
  15.         newVel.x = horizontalVelocityMagnitude * unitDirection.x;
  16.         newVel.z = horizontalVelocityMagnitude * unitDirection.z;
  17.         return newVel;
  18.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement