GibTreaty

Jump

May 20th, 2024 (edited)
574
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.88 KB | None | 1 0
  1. using UnityEngine;
  2. using UnityEngine.Events;
  3.  
  4. public class Jump : Ability {
  5.  
  6.     [field: SerializeField] public int MaxJumps { get; set; } = 1;
  7.     [field: SerializeField] public float Speed { get; set; } = 6;
  8.     [field: SerializeField] public float ExtraSpeed { get; set; } = 3;
  9.     [field: SerializeField] public float ExtraJumpDuration { get; set; } = 1;
  10.     [field: SerializeField] public float CoyoteTime { get; set; } = .15f;
  11.     [field: SerializeField] public float AngleEffectiveness { get; set; } = 1;
  12.     [field: SerializeField] public AnimationCurve SteepnessCurve { get; set; } = AnimationCurve.Linear(0, 1, 1, 0);
  13.     [field: SerializeField] public float Cooldown { get; set; } = .5f;
  14.     [field: SerializeField] public float CompressionRestDuration { get; set; } = .4f;
  15.     [field: SerializeField] public float CompressionLimit { get; set; } = .2f;
  16.  
  17.     public UnityEvent OnJump = new();
  18.     public GroundDetection Ground { get; private set; }
  19.     public int JumpCount { get; private set; }
  20.     public bool JumpInput { get; set; }
  21.     public float LastJumpTime { get; private set; }
  22.     public Rigidbody Rigidbody { get; private set; }
  23.  
  24.     Vector3 jumpDirection;
  25.     bool inAir;
  26.     bool jumpReleased;
  27.     bool stoppedExtraJump;
  28.  
  29.     float lastCompressionTime;
  30.  
  31.     void Awake() {
  32.         Ground = GetComponent<GroundDetection>();
  33.         Rigidbody = GetComponent<Rigidbody>();
  34.     }
  35.  
  36.     void FixedUpdate() {
  37.         UpdateCompressionCheck();
  38.         UpdateJumped();
  39.         UpdateJump();
  40.     }
  41.  
  42.     void Update() {
  43.         //JumpInput = Input.GetKey(KeyCode.Space);
  44.  
  45.         if(inAir && !JumpInput)
  46.             jumpReleased = true;
  47.     }
  48.  
  49.     void UpdateCompressionCheck() {
  50.         if(Ground.Compression <= CompressionLimit && Ground.IsStanding) return;
  51.  
  52.         lastCompressionTime = Time.time;
  53.     }
  54.  
  55.     void UpdateJump() {
  56.         if(JumpCount == 0 && !Ground.IsGrounded && !inAir) {
  57.             if(MaxJumps > 1) {
  58.                 JumpCount = 1;
  59.                 inAir = true;
  60.                 jumpReleased = true;
  61.                 stoppedExtraJump = true;
  62.                 JumpInput = false;
  63.             }
  64.         }
  65.  
  66.         if(!Usable) return;
  67.         if(!JumpInput) return;
  68.         if(inAir && JumpCount >= MaxJumps) return;
  69.         if(Time.time - LastJumpTime < Cooldown && LastJumpTime != 0) return;
  70.  
  71.         if(Ground.IsGrounded)
  72.             if(Time.time - lastCompressionTime < CompressionRestDuration) return;
  73.  
  74.         if(JumpCount >= 1 && !jumpReleased) return;
  75.  
  76.         if(JumpCount == 0 && MaxJumps == 1)
  77.             if(Time.time - Ground.LastGroundedTime >= CoyoteTime) return;
  78.  
  79.         var velocity = Rigidbody.velocity;
  80.         var jumpNormal = Ground.IsGrounded ? Ground.LastGroundedHit.normal : Vector3.up;
  81.  
  82.         float steepness = Mathf.Max(SteepnessCurve.Evaluate(jumpNormal.y), 0);
  83.  
  84.         jumpDirection = Vector3.Scale(jumpNormal, new Vector3(AngleEffectiveness, steepness, AngleEffectiveness));
  85.         velocity.y = 0;
  86.         velocity += (jumpDirection * Speed).normalized * Speed;
  87.         Rigidbody.velocity = velocity;
  88.  
  89.         LastJumpTime = Time.time;
  90.         JumpCount++;
  91.         inAir = true;
  92.         jumpReleased = false;
  93.         stoppedExtraJump = false;
  94.  
  95.         OnJump?.Invoke();
  96.     }
  97.  
  98.     void UpdateJumped() {
  99.         if(!inAir) return;
  100.  
  101.         var jumpTime = Time.time - LastJumpTime;
  102.         var extraJumpExpired = jumpTime > ExtraJumpDuration;
  103.  
  104.         if(!stoppedExtraJump && (!JumpInput || extraJumpExpired)) {
  105.             stoppedExtraJump = true;
  106.             JumpInput = false;
  107.         }
  108.  
  109.         if(Ground.IsGrounded && Rigidbody.velocity.y <= 0) {
  110.             inAir = false;
  111.             jumpReleased = false;
  112.             JumpCount = 0;
  113.             return;
  114.         }
  115.  
  116.         if(stoppedExtraJump) return;
  117.  
  118.         Rigidbody.AddForce(jumpDirection * ExtraSpeed, ForceMode.Acceleration);
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment