From c33c94990c8db5a56a5947f5955d30b5f41e5a35 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sun, 3 May 2020 17:54:19 -0500 Subject: [PATCH] ok maybe just single jump --- Assets/Scenes/SampleScene.unity | 9 ++++-- Assets/Scripts/PlayerController.cs | 52 ++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 4c5e657..169a537 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -7471,7 +7471,7 @@ PrefabInstance: - target: {fileID: 4623847142764859891, guid: b29a944aaba25f643afdc6b049845662, type: 3} propertyPath: timeToJumpApex - value: 0.3 + value: 0.25 objectReference: {fileID: 0} - target: {fileID: 4623847142764859891, guid: b29a944aaba25f643afdc6b049845662, type: 3} @@ -7491,7 +7491,7 @@ PrefabInstance: - target: {fileID: 4623847142764859891, guid: b29a944aaba25f643afdc6b049845662, type: 3} propertyPath: maxFallSpeed - value: -30 + value: -40 objectReference: {fileID: 0} - target: {fileID: 4623847142764859891, guid: b29a944aaba25f643afdc6b049845662, type: 3} @@ -7513,5 +7513,10 @@ PrefabInstance: propertyPath: timeToMaxAirmoveSpeed value: 0.25 objectReference: {fileID: 0} + - target: {fileID: 4623847142764859891, guid: b29a944aaba25f643afdc6b049845662, + type: 3} + propertyPath: maxJumps + value: 1 + objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: b29a944aaba25f643afdc6b049845662, type: 3} diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index 13c218b..aa9011f 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -14,6 +14,7 @@ public class PlayerController : MonoBehaviour { public float timeToMaxAirmoveSpeed = 0.025f; public float maxApexTime = 0.05f; // amount of time spent at max jump height before falling again public float coyoteTime = 0.1f; + public int maxJumps = 2; public GameObject tracerPrefab; // this has to be public to be readable by the display, which is a code @@ -23,6 +24,7 @@ public class PlayerController : MonoBehaviour { private float jumpStateStart; private float jumpStartTime; private float jumpHeldDuration; + public int jumpCount; private bool wasHoldingJump; public float apexTime; @@ -73,6 +75,7 @@ public class PlayerController : MonoBehaviour { velocity.x = 0f; } setJumpState(JumpState.Ascending); + break; } else { velocity.y = gravity * Time.deltaTime; velocity.x = Mathf.SmoothDamp(velocity.x, targetX, ref velocityXSmoothing, timeToMaxRunSpeed); @@ -88,7 +91,18 @@ public class PlayerController : MonoBehaviour { // the drag coefficient gets bigger as we ascend, terminating at 1 float dragCoefficient = n * n; - if (Input.GetButton("Jump")) { + if (Input.GetButtonDown("Jump") && jumpCount < maxJumps) { + velocity.y = jumpVelocity; + if (input.x >= 0.25f) { + velocity.x = moveSpeed*maxForwardJumpBoost; + } else if (input.x <= -0.25f) { + velocity.x = -moveSpeed*maxForwardJumpBoost; + } else { + velocity.x = 0f; + } + setJumpState(JumpState.Ascending); + break; + } else if (Input.GetButton("Jump")) { float jumpDrag = jumpVelocity * dragCoefficient; if (n <= 0.7) { jumpDrag = 0; @@ -137,7 +151,7 @@ public class PlayerController : MonoBehaviour { case JumpState.Apex: tracer.color = Color.magenta; - if (Input.GetButtonDown("Jump")) { + if (Input.GetButtonDown("Jump") && jumpCount < maxJumps) { velocity.y = jumpVelocity; if (input.x >= 0.25f) { velocity.x = moveSpeed*maxForwardJumpBoost; @@ -147,6 +161,7 @@ public class PlayerController : MonoBehaviour { velocity.x = 0f; } setJumpState(JumpState.Ascending); + break; } else { switch (jumpDirection) { case JumpDirection.Neutral: @@ -174,6 +189,19 @@ public class PlayerController : MonoBehaviour { case JumpState.Descending: tracer.color = Color.yellow; + if (Input.GetButtonDown("Jump") && jumpCount < maxJumps) { + velocity.y = jumpVelocity; + if (input.x >= 0.25f) { + velocity.x = moveSpeed*maxForwardJumpBoost; + } else if (input.x <= -0.25f) { + velocity.x = -moveSpeed*maxForwardJumpBoost; + } else { + velocity.x = 0f; + } + setJumpState(JumpState.Ascending); + break; + } + float n2 = (Time.time - jumpStateStart) / timeToJumpApex; n2 = Mathf.Clamp(n2, 0, 1); switch (jumpDirection) { @@ -211,6 +239,19 @@ public class PlayerController : MonoBehaviour { case JumpState.Falling: tracer.color = Color.red; + if (Input.GetButtonDown("Jump") && jumpCount < maxJumps) { + velocity.y = jumpVelocity; + if (input.x >= 0.25f) { + velocity.x = moveSpeed*maxForwardJumpBoost; + } else if (input.x <= -0.25f) { + velocity.x = -moveSpeed*maxForwardJumpBoost; + } else { + velocity.x = 0f; + } + setJumpState(JumpState.Ascending); + break; + } + switch (jumpDirection) { case JumpDirection.Neutral: velocity.x = Mathf.SmoothDamp(velocity.x, targetX, ref velocityXSmoothing, timeToMaxAirmoveSpeed); @@ -265,6 +306,13 @@ public class PlayerController : MonoBehaviour { } else { jumpDirection = JumpDirection.Left; } + + if (state == JumpState.Grounded) { + jumpCount = 0; + } + if (state == JumpState.Ascending) { + jumpCount++; + } } /*