Advertisement
GibTreaty

Jump

May 20th, 2024
489
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.11 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.     //float baseGroundDistance;
  29.     //float baseStandDistance;
  30.  
  31.     float lastCompressionTime;
  32.  
  33.     void Awake() {
  34.         Ground = GetComponent<GroundDetection>();
  35.         Rigidbody = GetComponent<Rigidbody>();
  36.  
  37.         //baseGroundDistance = Ground.GroundedDistance;
  38.         //baseStandDistance = Ground.StandDistance;
  39.     }
  40.  
  41.     void FixedUpdate() {
  42.         UpdateCompressionCheck();
  43.         UpdateJumped();
  44.         UpdateJump();
  45.     }
  46.  
  47.     void Update() {
  48.         //JumpInput = Input.GetKey(KeyCode.Space);
  49.  
  50.         if(inAir && !JumpInput)
  51.             jumpReleased = true;
  52.     }
  53.  
  54.     void UpdateCompressionCheck() {
  55.         if(Ground.Compression <= CompressionLimit && Ground.IsStanding) return;
  56.  
  57.         lastCompressionTime = Time.time;
  58.     }
  59.  
  60.     void UpdateJump() {
  61.         if(JumpCount == 0 && !Ground.IsGrounded && !inAir) {
  62.             if(MaxJumps > 1) {
  63.                 JumpCount = 1;
  64.                 inAir = true;
  65.                 jumpReleased = true;
  66.                 stoppedExtraJump = true;
  67.                 JumpInput = false;
  68.             }
  69.         }
  70.  
  71.         if(!Usable) return;
  72.         if(!JumpInput) return;
  73.         if(inAir && JumpCount >= MaxJumps) return;
  74.         if(Time.time - LastJumpTime < Cooldown && LastJumpTime != 0) return;
  75.  
  76.         if(Ground.IsGrounded)
  77.             if(Time.time - lastCompressionTime < CompressionRestDuration) return;
  78.  
  79.         if(JumpCount >= 1 && !jumpReleased) return;
  80.  
  81.         if(JumpCount == 0 && MaxJumps == 1)
  82.             if(Time.time - Ground.LastGroundedTime >= CoyoteTime) return;
  83.  
  84.         var velocity = Rigidbody.velocity;
  85.         var jumpNormal = Ground.IsGrounded ? Ground.LastGroundedHit.normal : Vector3.up;
  86.  
  87.         //if(SteepnessCurve.Evaluate(jumpNormal.y)
  88.  
  89.         float steepness = Mathf.Max(SteepnessCurve.Evaluate(jumpNormal.y), 0);
  90.  
  91.         jumpDirection = Vector3.Scale(jumpNormal, new Vector3(AngleEffectiveness, steepness, AngleEffectiveness));
  92.         velocity.y = 0;
  93.         velocity += (jumpDirection * Speed).normalized * Speed;
  94.         Rigidbody.velocity = velocity;
  95.  
  96.         LastJumpTime = Time.time;
  97.         JumpCount++;
  98.         inAir = true;
  99.         jumpReleased = false;
  100.         stoppedExtraJump = false;
  101.  
  102.         OnJump?.Invoke();
  103.  
  104.     }
  105.  
  106.     void UpdateJumped() {
  107.         if(!inAir) return;
  108.  
  109.         var jumpTime = Time.time - LastJumpTime;
  110.         var extraJumpExpired = jumpTime > ExtraJumpDuration;
  111.  
  112.         if(!stoppedExtraJump && (!JumpInput || extraJumpExpired)) {
  113.             stoppedExtraJump = true;
  114.             JumpInput = false;
  115.         }
  116.  
  117.         if(Ground.IsGrounded && Rigidbody.velocity.y <= 0) {
  118.             inAir = false;
  119.             jumpReleased = false;
  120.             JumpCount = 0;
  121.             return;
  122.         }
  123.  
  124.         if(stoppedExtraJump) return;
  125.  
  126.         Rigidbody.AddForce(jumpDirection * ExtraSpeed, ForceMode.Acceleration);
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement