apex time is a function of jump held time

master
Jordan Orelli 4 years ago
parent 63360b4dd3
commit 0c7a6689cb

@ -6859,12 +6859,17 @@ PrefabInstance:
- target: {fileID: 4623847142764859891, guid: b29a944aaba25f643afdc6b049845662, - target: {fileID: 4623847142764859891, guid: b29a944aaba25f643afdc6b049845662,
type: 3} type: 3}
propertyPath: maxApexTime propertyPath: maxApexTime
value: 0.1 value: 0.3
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 4623847142764859891, guid: b29a944aaba25f643afdc6b049845662, - target: {fileID: 4623847142764859891, guid: b29a944aaba25f643afdc6b049845662,
type: 3} type: 3}
propertyPath: maxForwardJumpBoost propertyPath: maxForwardJumpBoost
value: 2 value: 2
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 4623847142764859891, guid: b29a944aaba25f643afdc6b049845662,
type: 3}
propertyPath: timeToMaxAirmoveSpeed
value: 0.25
objectReference: {fileID: 0}
m_RemovedComponents: [] m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: b29a944aaba25f643afdc6b049845662, type: 3} m_SourcePrefab: {fileID: 100100000, guid: b29a944aaba25f643afdc6b049845662, type: 3}

@ -19,8 +19,12 @@ public class PlayerController : MonoBehaviour {
// this has to be public to be readable by the display, which is a code // this has to be public to be readable by the display, which is a code
// smell. // smell.
public JumpState jumpState; public JumpState jumpState;
public JumpDirection jumpDirection;
private float jumpStateStart; private float jumpStateStart;
private float apexTime; private float jumpStartTime;
private float jumpHeldDuration;
private bool wasHoldingJump;
public float apexTime;
private float jumpVelocity = 8f; private float jumpVelocity = 8f;
private float gravity = -20f; private float gravity = -20f;
@ -36,7 +40,9 @@ public class PlayerController : MonoBehaviour {
} }
void Update() { void Update() {
Instantiate(tracerPrefab, transform.position, Quaternion.identity); GameObject tracerObj = Instantiate(tracerPrefab, transform.position, Quaternion.identity);
TracerDot tracer = tracerObj.GetComponent<TracerDot>();
Debug.Log(tracer);
Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
float targetX = input.x * moveSpeed; float targetX = input.x * moveSpeed;
@ -55,7 +61,9 @@ public class PlayerController : MonoBehaviour {
switch (jumpState) { switch (jumpState) {
case JumpState.Grounded: case JumpState.Grounded:
if (Input.GetButtonDown("Jump")) { tracer.color = Color.white;
if (Input.GetButton("Jump")) {
velocity.y = jumpVelocity; velocity.y = jumpVelocity;
if (input.x >= 0.25f) { if (input.x >= 0.25f) {
velocity.x = moveSpeed*maxForwardJumpBoost; velocity.x = moveSpeed*maxForwardJumpBoost;
@ -72,56 +80,63 @@ public class PlayerController : MonoBehaviour {
break; break;
case JumpState.Ascending: case JumpState.Ascending:
tracer.color = Color.green;
// n starts at 0 and goes to 1 as we ascend
float n = Mathf.InverseLerp(0, timeToJumpApex, Time.time - jumpStateStart); float n = Mathf.InverseLerp(0, timeToJumpApex, Time.time - jumpStateStart);
// the drag coefficient gets bigger as we ascend, terminating at 1
float dragCoefficient = n * n; float dragCoefficient = n * n;
if (Input.GetButton("Jump")) { if (Input.GetButton("Jump")) {
float jumpDrag = jumpVelocity * dragCoefficient; float jumpDrag = jumpVelocity * dragCoefficient;
if (n <= 0.4) { if (n <= 0.7) {
jumpDrag = 0; jumpDrag = 0;
} }
velocity.y = jumpVelocity - jumpDrag; velocity.y = jumpVelocity - jumpDrag;
} else { } else {
// if we're not pressing jump any more we add all the gravity // if we're not pressing jump any more we add all the gravity
velocity.y += gravity * Time.deltaTime; velocity.y += 4 * gravity * Time.deltaTime;
// the amount of time spent floating is equal to the amount of
// time the player held down the jump button.
if (wasHoldingJump) {
apexTime = Time.time - jumpStartTime;
}
} }
float forwardBoost = maxForwardJumpBoost - (maxForwardJumpBoost * dragCoefficient); float forwardBoost = maxForwardJumpBoost - (maxForwardJumpBoost * dragCoefficient);
forwardBoost = Mathf.Clamp(forwardBoost, 1, maxForwardJumpBoost); forwardBoost = Mathf.Clamp(forwardBoost, 1, maxForwardJumpBoost);
if (velocity.x >= 0) { switch (jumpDirection) {
if (targetX >= 0) { case JumpDirection.Neutral:
// continuing to move in your current direction is a boost // you can wiggle out of a neutral jump in either direction
velocity.x = Mathf.SmoothDamp(velocity.x, targetX * forwardBoost, ref velocityXSmoothing, timeToMaxAirmoveSpeed); velocity.x = Mathf.SmoothDamp(velocity.x, targetX, ref velocityXSmoothing, timeToMaxAirmoveSpeed);
} else { break;
// moving in the opposite direction can slow you down but
// not turn you around case JumpDirection.Left:
velocity.x = Mathf.SmoothDamp(velocity.x, targetX, ref velocityXSmoothing, timeToMaxAirmoveSpeed); velocity.x = Mathf.SmoothDamp(velocity.x, targetX < 0 ? targetX * forwardBoost : 0, ref velocityXSmoothing, timeToMaxAirmoveSpeed);
velocity.x = Mathf.Clamp(velocity.x, 0, initialVelocity.x); break;
}
} else { case JumpDirection.Right:
if (targetX <= 0) { velocity.x = Mathf.SmoothDamp(velocity.x, targetX > 0 ? targetX * forwardBoost : 0, ref velocityXSmoothing, timeToMaxAirmoveSpeed);
// continuing to move in your current direction is a boost break;
velocity.x = Mathf.SmoothDamp(velocity.x, targetX * forwardBoost, ref velocityXSmoothing, timeToMaxAirmoveSpeed);
} else {
// moving in the opposite direction can slow you down but
// not turn you around
velocity.x = Mathf.SmoothDamp(velocity.x, targetX, ref velocityXSmoothing, timeToMaxAirmoveSpeed);
velocity.x = Mathf.Clamp(velocity.x, initialVelocity.x, 0);
}
} }
// if we were rising in the last frame but will be falling in this // if we were rising in the last frame but will be falling in this
// frame, we should zero out the velocity to float instead. // frame, we should zero out the velocity to float instead.
if (initialVelocity.y >= 0 && velocity.y <= 0) { if (initialVelocity.y >= 0 && velocity.y <= 0) {
velocity.y = 0; velocity.y = 0;
if (wasHoldingJump && Input.GetButton("Jump")) {
apexTime = maxApexTime;
}
setJumpState(JumpState.Apex); setJumpState(JumpState.Apex);
apexTime = Mathf.Clamp(Time.time - jumpStateStart, 0, maxApexTime);
} }
break; break;
case JumpState.Apex: case JumpState.Apex:
tracer.color = Color.magenta;
if (Input.GetButtonDown("Jump")) { if (Input.GetButtonDown("Jump")) {
velocity.y = jumpVelocity; velocity.y = jumpVelocity;
if (input.x >= 0.25f) { if (input.x >= 0.25f) {
@ -133,10 +148,20 @@ public class PlayerController : MonoBehaviour {
} }
setJumpState(JumpState.Ascending); setJumpState(JumpState.Ascending);
} else { } else {
// your horizontal motion at apex is constant throughout switch (jumpDirection) {
// velocity.x = Mathf.SmoothDamp(velocity.x, targetX, ref velocityXSmoothing, timeToMaxAirmoveSpeed); case JumpDirection.Neutral:
velocity.x = Mathf.SmoothDamp(velocity.x, targetX, ref velocityXSmoothing, timeToMaxAirmoveSpeed);
break;
case JumpDirection.Left:
velocity.x = Mathf.SmoothDamp(velocity.x, targetX < 0 ? targetX : 0, ref velocityXSmoothing, timeToMaxAirmoveSpeed);
break;
case JumpDirection.Right:
velocity.x = Mathf.SmoothDamp(velocity.x, targetX > 0 ? targetX : 0, ref velocityXSmoothing, timeToMaxAirmoveSpeed);
break;
}
float timeAtApex = Time.time - jumpStateStart; float timeAtApex = Time.time - jumpStateStart;
float apexTimeRemaining = maxApexTime - timeAtApex; float apexTimeRemaining = apexTime - timeAtApex;
if (apexTimeRemaining < 0) { if (apexTimeRemaining < 0) {
velocity.y += gravity * -apexTimeRemaining; velocity.y += gravity * -apexTimeRemaining;
setJumpState(JumpState.Descending); setJumpState(JumpState.Descending);
@ -147,25 +172,28 @@ public class PlayerController : MonoBehaviour {
break; break;
case JumpState.Descending: case JumpState.Descending:
tracer.color = Color.yellow;
float n2 = (Time.time - jumpStateStart) / timeToJumpApex; float n2 = (Time.time - jumpStateStart) / timeToJumpApex;
n2 = Mathf.Clamp(n2, 0, 1); n2 = Mathf.Clamp(n2, 0, 1);
switch (jumpDirection) {
// horizontal travel is decreasing when descending, so that you case JumpDirection.Neutral:
// always land vertically. Drag increases as you descend, so that it velocity.x = Mathf.SmoothDamp(velocity.x, targetX, ref velocityXSmoothing, timeToMaxAirmoveSpeed);
// is 1 at the end of your descent. break;
// float drag = n2*n2*Mathf.Abs(velocity.x); case JumpDirection.Left:
// drag = 0; velocity.x = Mathf.SmoothDamp(velocity.x, targetX < 0 ? targetX : 0, ref velocityXSmoothing, timeToMaxAirmoveSpeed);
// if (velocity.x >= 0) { break;
// velocity.x = Mathf.SmoothDamp(velocity.x, Mathf.Clamp(targetX, 0, velocity.x-drag), ref velocityXSmoothing, timeToMaxAirmoveSpeed); case JumpDirection.Right:
// } else { velocity.x = Mathf.SmoothDamp(velocity.x, targetX > 0 ? targetX : 0, ref velocityXSmoothing, timeToMaxAirmoveSpeed);
// velocity.x = Mathf.SmoothDamp(velocity.x, Mathf.Clamp(targetX, velocity.x+drag, 0), ref velocityXSmoothing, timeToMaxAirmoveSpeed); break;
// } }
// fall speed is increasing when descending // fall speed is increasing when descending
velocity.y += gravity * Time.deltaTime * n2 * n2; velocity.y += gravity * Time.deltaTime * (Mathf.Clamp(4 * n2 * n2, 1, 4));
break; break;
case JumpState.CoyoteTime: case JumpState.CoyoteTime:
tracer.color = Color.blue;
velocity.x = Mathf.SmoothDamp(velocity.x, targetX, ref velocityXSmoothing, timeToMaxRunSpeed); velocity.x = Mathf.SmoothDamp(velocity.x, targetX, ref velocityXSmoothing, timeToMaxRunSpeed);
if (Input.GetButtonDown("Jump")) { if (Input.GetButtonDown("Jump")) {
setJumpState(JumpState.Ascending); setJumpState(JumpState.Ascending);
@ -181,7 +209,19 @@ public class PlayerController : MonoBehaviour {
break; break;
case JumpState.Falling: case JumpState.Falling:
velocity.x = Mathf.SmoothDamp(velocity.x, targetX, ref velocityXSmoothing, timeToMaxAirmoveSpeed); tracer.color = Color.red;
switch (jumpDirection) {
case JumpDirection.Neutral:
velocity.x = Mathf.SmoothDamp(velocity.x, targetX, ref velocityXSmoothing, timeToMaxAirmoveSpeed);
break;
case JumpDirection.Left:
velocity.x = Mathf.SmoothDamp(velocity.x, targetX < 0 ? targetX : 0, ref velocityXSmoothing, timeToMaxAirmoveSpeed);
break;
case JumpDirection.Right:
velocity.x = Mathf.SmoothDamp(velocity.x, targetX > 0 ? targetX : 0, ref velocityXSmoothing, timeToMaxAirmoveSpeed);
break;
}
velocity.y += gravity * Time.deltaTime; velocity.y += gravity * Time.deltaTime;
break; break;
@ -200,6 +240,7 @@ public class PlayerController : MonoBehaviour {
if (jumpState != JumpState.Grounded && moveController.isGrounded) { if (jumpState != JumpState.Grounded && moveController.isGrounded) {
setJumpState(JumpState.Grounded); setJumpState(JumpState.Grounded);
} }
wasHoldingJump = Input.GetButton("Jump");
} }
void OnDestroy() { void OnDestroy() {
@ -212,8 +253,18 @@ public class PlayerController : MonoBehaviour {
} }
void setJumpState(JumpState state) { void setJumpState(JumpState state) {
if (jumpState != JumpState.Ascending && state == JumpState.Ascending) {
jumpStartTime = Time.time;
}
jumpState = state; jumpState = state;
jumpStateStart = Time.time; jumpStateStart = Time.time;
if (velocity.x == 0) {
jumpDirection = JumpDirection.Neutral;
} else if (velocity.x > 0) {
jumpDirection = JumpDirection.Right;
} else {
jumpDirection = JumpDirection.Left;
}
} }
/* /*
@ -251,4 +302,10 @@ public class PlayerController : MonoBehaviour {
// did not initially jump. // did not initially jump.
Falling, Falling,
} }
public enum JumpDirection {
Neutral,
Left,
Right,
}
} }

@ -4,13 +4,15 @@ using UnityEngine;
public class TracerDot : MonoBehaviour { public class TracerDot : MonoBehaviour {
private float spawned; private float spawned;
public Color color;
void Start() { void Start() {
spawned = Time.time; spawned = Time.time;
// color = Color.white;
} }
void OnDrawGizmos() { void OnDrawGizmos() {
Gizmos.color = Color.red; Gizmos.color = color;
Gizmos.DrawWireSphere(transform.position, 0.125f); Gizmos.DrawWireSphere(transform.position, 0.125f);
} }

Loading…
Cancel
Save