idlemouse

UnityJump

Sep 5th, 2024
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.65 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. [RequireComponent(typeof(Rigidbody))]
  4. public class JumpDistanceController : MonoBehaviour
  5. {
  6.     private Rigidbody rigidbody;
  7.     [SerializeField] private float jumpHeight = 5;
  8.     [SerializeField] private float jumpXDistance = 0;
  9.     [SerializeField] private float jumpZDistance = 0;
  10.     private float resolvedJumpYInitialVelocity = 0f;
  11.     private float resolvedJumpTime = 0f;
  12.     private float resolvedJumpXInitialVelocity, resolvedJumpZInitialVelocity = 0f;
  13.     private float startHeight, maxHeight = 0f;
  14.     private float startX, maxX = 0f;
  15.     private float startZ, maxZ = 0f;
  16.  
  17.     // Start is called once before the first execution of Update after the MonoBehaviour is created
  18.     void Start()
  19.     {
  20.         rigidbody = GetComponent<Rigidbody>();
  21.         animationController = GetComponent<AnimationController>();
  22.  
  23.         // Stats
  24.         startHeight = transform.position.y;
  25.         startX = transform.position.x;
  26.         startZ = transform.position.z;
  27.     }
  28.  
  29.     // Update is called once per frame
  30.     void Update()
  31.     {
  32.         if (Input.GetKeyDown(KeyCode.J))
  33.         {
  34.             // Resolve velocities
  35.             resolvedJumpYInitialVelocity = JumpYVelocity(jumpHeight);
  36.             resolvedJumpTime = JumpTime(resolvedJumpYInitialVelocity);
  37.             resolvedJumpXInitialVelocity = JumpHorzVelocity(resolvedJumpTime, jumpXDistance);
  38.             resolvedJumpZInitialVelocity = JumpHorzVelocity(resolvedJumpTime, jumpZDistance);
  39.  
  40.             rigidbody.AddForce(new Vector3(
  41.                     ForceFromVelocity(resolvedJumpXInitialVelocity),
  42.                     ForceFromVelocity(resolvedJumpYInitialVelocity),
  43.                     ForceFromVelocity(resolvedJumpZInitialVelocity)),
  44.                 ForceMode.Impulse);
  45.         }
  46.  
  47.         // Stats
  48.         float currentHeight = transform.position.y - startHeight;
  49.         if (currentHeight > maxHeight)
  50.         { maxHeight = currentHeight; }
  51.  
  52.         float currentX = transform.position.x - startX;
  53.         if (currentX > maxX)
  54.         { maxX = currentX; }
  55.  
  56.         float currentZ = transform.position.z - startZ;
  57.         if (currentZ > maxZ) {maxZ = currentZ; }
  58.         //Debug.Log("maxHeight, maxDistance: " + maxHeight + "," + maxDistance);
  59.     }
  60.  
  61.     private float JumpYVelocity(float resolvedHeight)
  62.     {
  63.         float yVelocity = 0f;
  64.  
  65.         // 3rd equation of motion:
  66.         //      v^2 = v0^2 + 2a(s - s0)
  67.         //      v0  = sqrt(0 - 2a(s - s0))
  68.         yVelocity = Mathf.Sqrt(-2 * Physics.gravity.y * resolvedHeight);
  69.  
  70.         return yVelocity;
  71.     }
  72.  
  73.     private float JumpTime(float jumpStartVeclocity)
  74.     {
  75.         float halfTime = 0f; // time from start to jump peak
  76.         float time = 0f;
  77.  
  78.         // 1st equation of motion:
  79.         //      v = v0 + at
  80.         //      t = (0 - v0) / a
  81.         halfTime = - jumpStartVeclocity / Physics.gravity.y;
  82.         time = halfTime * 2;
  83.  
  84.         return time;
  85.     }
  86.  
  87.     private float JumpHorzVelocity(float jumpTotalTime, float jumpDistance)
  88.     {
  89.         float xVelocity = 0f;
  90.  
  91.         // average velocity = distance / time
  92.         xVelocity = jumpDistance / jumpTotalTime;
  93.  
  94.         return xVelocity;
  95.     }
  96.  
  97.     private float ForceFromVelocity(float velocity)
  98.     {
  99.         return velocity * rigidbody.mass;
  100.     }
  101.  
  102.     public float GetMaxY() { return maxHeight; }
  103.     public float GetMaxX() { return maxX; }
  104.     public float GetMaxZ() { return maxZ; }
  105.     public float GetCurrentY() { return transform.position.y - startHeight;; }
  106.     public float GetCurrentX() { return transform.position.x - startX;; }
  107.     public float GetCurrentZ() { return transform.position.z - startZ;; }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment