Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- [RequireComponent(typeof(Rigidbody))]
- public class JumpDistanceController : MonoBehaviour
- {
- private Rigidbody rigidbody;
- [SerializeField] private float jumpHeight = 5;
- [SerializeField] private float jumpXDistance = 0;
- [SerializeField] private float jumpZDistance = 0;
- private float resolvedJumpYInitialVelocity = 0f;
- private float resolvedJumpTime = 0f;
- private float resolvedJumpXInitialVelocity, resolvedJumpZInitialVelocity = 0f;
- private float startHeight, maxHeight = 0f;
- private float startX, maxX = 0f;
- private float startZ, maxZ = 0f;
- // Start is called once before the first execution of Update after the MonoBehaviour is created
- void Start()
- {
- rigidbody = GetComponent<Rigidbody>();
- animationController = GetComponent<AnimationController>();
- // Stats
- startHeight = transform.position.y;
- startX = transform.position.x;
- startZ = transform.position.z;
- }
- // Update is called once per frame
- void Update()
- {
- if (Input.GetKeyDown(KeyCode.J))
- {
- // Resolve velocities
- resolvedJumpYInitialVelocity = JumpYVelocity(jumpHeight);
- resolvedJumpTime = JumpTime(resolvedJumpYInitialVelocity);
- resolvedJumpXInitialVelocity = JumpHorzVelocity(resolvedJumpTime, jumpXDistance);
- resolvedJumpZInitialVelocity = JumpHorzVelocity(resolvedJumpTime, jumpZDistance);
- rigidbody.AddForce(new Vector3(
- ForceFromVelocity(resolvedJumpXInitialVelocity),
- ForceFromVelocity(resolvedJumpYInitialVelocity),
- ForceFromVelocity(resolvedJumpZInitialVelocity)),
- ForceMode.Impulse);
- }
- // Stats
- float currentHeight = transform.position.y - startHeight;
- if (currentHeight > maxHeight)
- { maxHeight = currentHeight; }
- float currentX = transform.position.x - startX;
- if (currentX > maxX)
- { maxX = currentX; }
- float currentZ = transform.position.z - startZ;
- if (currentZ > maxZ) {maxZ = currentZ; }
- //Debug.Log("maxHeight, maxDistance: " + maxHeight + "," + maxDistance);
- }
- private float JumpYVelocity(float resolvedHeight)
- {
- float yVelocity = 0f;
- // 3rd equation of motion:
- // v^2 = v0^2 + 2a(s - s0)
- // v0 = sqrt(0 - 2a(s - s0))
- yVelocity = Mathf.Sqrt(-2 * Physics.gravity.y * resolvedHeight);
- return yVelocity;
- }
- private float JumpTime(float jumpStartVeclocity)
- {
- float halfTime = 0f; // time from start to jump peak
- float time = 0f;
- // 1st equation of motion:
- // v = v0 + at
- // t = (0 - v0) / a
- halfTime = - jumpStartVeclocity / Physics.gravity.y;
- time = halfTime * 2;
- return time;
- }
- private float JumpHorzVelocity(float jumpTotalTime, float jumpDistance)
- {
- float xVelocity = 0f;
- // average velocity = distance / time
- xVelocity = jumpDistance / jumpTotalTime;
- return xVelocity;
- }
- private float ForceFromVelocity(float velocity)
- {
- return velocity * rigidbody.mass;
- }
- public float GetMaxY() { return maxHeight; }
- public float GetMaxX() { return maxX; }
- public float GetMaxZ() { return maxZ; }
- public float GetCurrentY() { return transform.position.y - startHeight;; }
- public float GetCurrentX() { return transform.position.x - startX;; }
- public float GetCurrentZ() { return transform.position.z - startZ;; }
- }
Advertisement
Add Comment
Please, Sign In to add comment