Commit 61a84506 authored by Conner Stern's avatar Conner Stern

made dash use animationcurve

parent 24be4a20
......@@ -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<Jump>();
dashMeter = GameObject.FindGameObjectWithTag("DashMeter").transform;
coll = GetComponent<CircleCollider2D>();
walk = GetComponent<Walk>();
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));
}
......
......@@ -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;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment