Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.Events;
- public class Jump : Ability {
- [field: SerializeField] public int MaxJumps { get; set; } = 1;
- [field: SerializeField] public float Speed { get; set; } = 6;
- [field: SerializeField] public float ExtraSpeed { get; set; } = 3;
- [field: SerializeField] public float ExtraJumpDuration { get; set; } = 1;
- [field: SerializeField] public float CoyoteTime { get; set; } = .15f;
- [field: SerializeField] public float AngleEffectiveness { get; set; } = 1;
- [field: SerializeField] public AnimationCurve SteepnessCurve { get; set; } = AnimationCurve.Linear(0, 1, 1, 0);
- [field: SerializeField] public float Cooldown { get; set; } = .5f;
- [field: SerializeField] public float CompressionRestDuration { get; set; } = .4f;
- [field: SerializeField] public float CompressionLimit { get; set; } = .2f;
- public UnityEvent OnJump = new();
- public GroundDetection Ground { get; private set; }
- public int JumpCount { get; private set; }
- public bool JumpInput { get; set; }
- public float LastJumpTime { get; private set; }
- public Rigidbody Rigidbody { get; private set; }
- Vector3 jumpDirection;
- bool inAir;
- bool jumpReleased;
- bool stoppedExtraJump;
- //float baseGroundDistance;
- //float baseStandDistance;
- float lastCompressionTime;
- void Awake() {
- Ground = GetComponent<GroundDetection>();
- Rigidbody = GetComponent<Rigidbody>();
- //baseGroundDistance = Ground.GroundedDistance;
- //baseStandDistance = Ground.StandDistance;
- }
- void FixedUpdate() {
- UpdateCompressionCheck();
- UpdateJumped();
- UpdateJump();
- }
- void Update() {
- //JumpInput = Input.GetKey(KeyCode.Space);
- if(inAir && !JumpInput)
- jumpReleased = true;
- }
- void UpdateCompressionCheck() {
- if(Ground.Compression <= CompressionLimit && Ground.IsStanding) return;
- lastCompressionTime = Time.time;
- }
- void UpdateJump() {
- if(JumpCount == 0 && !Ground.IsGrounded && !inAir) {
- if(MaxJumps > 1) {
- JumpCount = 1;
- inAir = true;
- jumpReleased = true;
- stoppedExtraJump = true;
- JumpInput = false;
- }
- }
- if(!Usable) return;
- if(!JumpInput) return;
- if(inAir && JumpCount >= MaxJumps) return;
- if(Time.time - LastJumpTime < Cooldown && LastJumpTime != 0) return;
- if(Ground.IsGrounded)
- if(Time.time - lastCompressionTime < CompressionRestDuration) return;
- if(JumpCount >= 1 && !jumpReleased) return;
- if(JumpCount == 0 && MaxJumps == 1)
- if(Time.time - Ground.LastGroundedTime >= CoyoteTime) return;
- var velocity = Rigidbody.velocity;
- var jumpNormal = Ground.IsGrounded ? Ground.LastGroundedHit.normal : Vector3.up;
- //if(SteepnessCurve.Evaluate(jumpNormal.y)
- float steepness = Mathf.Max(SteepnessCurve.Evaluate(jumpNormal.y), 0);
- jumpDirection = Vector3.Scale(jumpNormal, new Vector3(AngleEffectiveness, steepness, AngleEffectiveness));
- velocity.y = 0;
- velocity += (jumpDirection * Speed).normalized * Speed;
- Rigidbody.velocity = velocity;
- LastJumpTime = Time.time;
- JumpCount++;
- inAir = true;
- jumpReleased = false;
- stoppedExtraJump = false;
- OnJump?.Invoke();
- }
- void UpdateJumped() {
- if(!inAir) return;
- var jumpTime = Time.time - LastJumpTime;
- var extraJumpExpired = jumpTime > ExtraJumpDuration;
- if(!stoppedExtraJump && (!JumpInput || extraJumpExpired)) {
- stoppedExtraJump = true;
- JumpInput = false;
- }
- if(Ground.IsGrounded && Rigidbody.velocity.y <= 0) {
- inAir = false;
- jumpReleased = false;
- JumpCount = 0;
- return;
- }
- if(stoppedExtraJump) return;
- Rigidbody.AddForce(jumpDirection * ExtraSpeed, ForceMode.Acceleration);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement