Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Find Velocity based on position calculation //
- private Vector3 findInitialVelocity(Vector3 startPosition, Vector3 finalPosition, float maxHeightOffset = 1.6f, float rangeOffset = 0.11f)
- {
- Vector3 newVel = new Vector3();
- Vector3 direction = new Vector3(finalPosition.x, 0f, finalPosition.z) - new Vector3(startPosition.x, 0f, startPosition.z);
- float range = direction.magnitude;
- range += rangeOffset;
- Vector3 unitDirection = direction.normalized;
- float maxYPos = finalPosition.y + maxHeightOffset;
- newVel.y = Mathf.Sqrt(-2.0f * Physics.gravity.y * (maxYPos - startPosition.y));
- float timeToMax = Mathf.Sqrt(-2.0f * (maxYPos - startPosition.y) / Physics.gravity.y);
- float timeToTargetY = Mathf.Sqrt(-2.0f * (maxYPos - finalPosition.y) / Physics.gravity.y);
- float totalFlightTime = (timeToMax / 10) + timeToTargetY;
- float horizontalVelocityMagnitude = range / totalFlightTime;
- newVel.x = horizontalVelocityMagnitude * unitDirection.x;
- newVel.z = horizontalVelocityMagnitude * unitDirection.z;
- return newVel;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement