From 61a84506c66566e69322c2042cdc9df73a9af630 Mon Sep 17 00:00:00 2001 From: Conner Stern Date: Thu, 6 Apr 2017 21:03:06 -0400 Subject: [PATCH] made dash use animationcurve --- Assets/Scripts/Birb/Dash.cs | 81 ++++++++++++++++++++++++++++++------- Assets/Scripts/Birb/Jump.cs | 3 ++ 2 files changed, 69 insertions(+), 15 deletions(-) diff --git a/Assets/Scripts/Birb/Dash.cs b/Assets/Scripts/Birb/Dash.cs index f6de634..bf049a8 100644 --- a/Assets/Scripts/Birb/Dash.cs +++ b/Assets/Scripts/Birb/Dash.cs @@ -7,10 +7,21 @@ using UnityEngine; public class Dash : AbstractBehavior { + //The velocity curve over time + [SerializeField] private AnimationCurve dashVelocityCurve; + private float distRatio { + get{ return dashVelocityCurve.keys[dashVelocityCurve.length - 1].time / dashDistance;} + } + private float distMultiplier = 1; + //The time of the dash + //[SerializeField] private float dashTime; + //Distance to dash + [SerializeField] private float dashDistance; + + Jump jumpScript; + public float coolDown = 1f; - public float dashforce = 5; public float holdTime = 0.4f; - public float minVelocity = 0.5f; private bool _isDashing = false; private Transform dashMeter; @@ -19,6 +30,8 @@ public class Dash : AbstractBehavior { private Walk walk; + private bool jumpDuringDash = false; + // Call to check if currently dashing public bool isDashing { @@ -30,9 +43,30 @@ public class Dash : AbstractBehavior { } void Start() { + jumpScript = GetComponent(); dashMeter = GameObject.FindGameObjectWithTag("DashMeter").transform; coll = GetComponent(); walk = GetComponent(); + + distMultiplier = ApproximateSpeedMultiplier(dashVelocityCurve, 100); + + //Setting up the v0 and a with Kenimatic Equations (Deprecated) + //dashVelocity = CalculateDashVelocity(dashDistance, dashTime, finalVelocity); + //dashDeceleration = CalculateDashDeceleration(finalVelocity, dashTime, dashVelocity); + } + + + float ApproximateSpeedMultiplier(AnimationCurve velocityOverTime, int samples){ + float time = velocityOverTime.Evaluate(velocityOverTime.keys[dashVelocityCurve.length - 1].time); + float distTraveled = 0; + + for(float i = 0; i < time; i += time / samples){ + distTraveled += velocityOverTime.Evaluate(i); + } + + distTraveled /= samples; + + return (dashDistance / distTraveled); } void Update() { @@ -41,6 +75,10 @@ public class Dash : AbstractBehavior { } UpdateUI(); animator.SetBool("is dashing", isDashing); + if (jumpScript._jumpRequested && isDashing){ + print("jumped"); + jumpDuringDash = true; + } } // Should be called every frame @@ -63,35 +101,37 @@ public class Dash : AbstractBehavior { if (walk != null) { walk.enabled = false; } - + //stores distance remaining in dash + float distRemaining = dashDistance; // dash direction vector Vector2 dir = ((int) direction) * Vector2.right; - // Cooldown lastDash = Time.time + coolDown; // Is dashing isDashing = true; - // Set initial velocity - body.velocity = dir * dashforce; // Disable gravity float initialGrav = body.gravityScale; body.gravityScale = 0; // results for circle cast RaycastHit2D[] results = new RaycastHit2D[8]; - // Decelerate - for (float i = 0; i < holdTime; i += Time.deltaTime) { - // Calculate velocity - Vector2 targetVel = (dir * dashforce) * (1 - (i / holdTime)); - - if(body.velocity.magnitude < minVelocity || collisionState.onWall) { + while(distRemaining >= 0) { + //Set initial velocity + float currentVel = dashVelocityCurve.Evaluate(distRatio * (dashDistance - distRemaining)); + //check if we need to stop dashing + if(distRemaining <= 0 || collisionState.onWall || jumpDuringDash) { // Velocity is low enough or collision with wall + jumpDuringDash = false; + print("ended dash"); break; } // Apply velocity - body.velocity = targetVel; + body.velocity = currentVel * dir; + print(body.velocity); + // Apply dist remaining reduciton + distRemaining -= currentVel * Time.deltaTime; float checkDistance = body.velocity.magnitude * Time.deltaTime; @@ -104,7 +144,7 @@ public class Dash : AbstractBehavior { breakable.Break(); } } - + // Wait yield return new WaitForEndOfFrame(); } @@ -118,7 +158,18 @@ public class Dash : AbstractBehavior { // Is not dashing isDashing = false; } - +/* + private float CalculateDashVelocity(float dashDist, float dashtTime, float finalVel){ + //manipulated equation "d = .5 * (v + v0) * t" to get v by itself + float velocity = 2 * (dashDist / dashTime) - finalVel; + return velocity; + } +*/ + private float CalculateDashDeceleration(float finalVel, float dashTime, float startVel){ + //manipulated equation v = v0 + at to get a by itself + float acceleration = (finalVel - startVel) / dashTime; + return acceleration; + } public void ActivateIgnoringCooldown(Directions direction) { StartCoroutine(DashRoutine(direction)); } diff --git a/Assets/Scripts/Birb/Jump.cs b/Assets/Scripts/Birb/Jump.cs index 52bec0a..13c06ec 100644 --- a/Assets/Scripts/Birb/Jump.cs +++ b/Assets/Scripts/Birb/Jump.cs @@ -10,6 +10,9 @@ public class Jump : AbstractBehavior { public float wallJumpHeight = 3; public float wallJumpHorizontalSpeed = 5; + public bool _jumpRequested{ + get{return jumpRequested;} + } bool jumpRequested; int airJumpsMade = 0; -- GitLab