From 2c2c53a3e3d8ac7b81d52ab7508ddf3b6540e5aa Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Wed, 20 May 2020 16:59:22 -0500 Subject: [PATCH] dash? --- Assets/Prefabs/Player.prefab | 106 ++++++++++++++++++++++++++++- Assets/Scripts/PlayerController.cs | 63 +++++++++++++++-- 2 files changed, 163 insertions(+), 6 deletions(-) diff --git a/Assets/Prefabs/Player.prefab b/Assets/Prefabs/Player.prefab index 52fe613..d5389dd 100644 --- a/Assets/Prefabs/Player.prefab +++ b/Assets/Prefabs/Player.prefab @@ -1,5 +1,99 @@ %YAML 1.1 %TAG !u! tag:unity3d.com,2011: +--- !u!1 &3504191955490326659 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 651885954300002501} + - component: {fileID: 8044712091767103009} + - component: {fileID: 70526350792130533} + - component: {fileID: 7913819140294386521} + m_Layer: 8 + m_Name: DashIndicator + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 0 +--- !u!4 &651885954300002501 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 3504191955490326659} + m_LocalRotation: {x: 0, y: 0, z: -0.38268343, w: 0.92387956} + m_LocalPosition: {x: 0.5, y: 0, z: 0} + m_LocalScale: {x: 0.5, y: 0.5, z: 0.5} + m_Children: [] + m_Father: {fileID: 4623847142764859880} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: -45} +--- !u!33 &8044712091767103009 +MeshFilter: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 3504191955490326659} + m_Mesh: {fileID: 10210, guid: 0000000000000000e000000000000000, type: 0} +--- !u!23 &70526350792130533 +MeshRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 3504191955490326659} + m_Enabled: 1 + m_CastShadows: 1 + m_ReceiveShadows: 1 + m_DynamicOccludee: 1 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RayTracingMode: 2 + m_RenderingLayerMask: 1 + m_RendererPriority: 0 + m_Materials: + - {fileID: 10303, guid: 0000000000000000f000000000000000, type: 0} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_ReceiveGI: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 1 + m_SelectedEditorRenderState: 3 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 +--- !u!64 &7913819140294386521 +MeshCollider: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 3504191955490326659} + m_Material: {fileID: 0} + m_IsTrigger: 0 + m_Enabled: 1 + serializedVersion: 3 + m_Convex: 0 + m_CookingOptions: 30 + m_Mesh: {fileID: 10210, guid: 0000000000000000e000000000000000, type: 0} --- !u!1 &4623847142764859890 GameObject: m_ObjectHideFlags: 0 @@ -31,7 +125,8 @@ Transform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 2, z: 1} - m_Children: [] + m_Children: + - {fileID: 651885954300002501} m_Father: {fileID: 0} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} @@ -50,15 +145,24 @@ MonoBehaviour: jumpHeight: 4 maxForwardJumpBoost: 2.5 timeToJumpApex: 0.4 + lastDashTime: 0 + dashTime: 0.125 + dashSpeed: 50 + dashCooldown: 1.25 moveSpeed: 6 maxFallSpeed: -20 timeToMaxRunSpeed: 0.15 timeToMaxAirmoveSpeed: 0.025 maxApexTime: 0.05 coyoteTime: 0.1 + maxJumps: 2 tracerPrefab: {fileID: 2438817463505096557, guid: 76ce4ec4aba832343abb5a3c19e66b7c, type: 3} + dashIndicator: {fileID: 3504191955490326659} jumpState: 0 + jumpDirection: 0 + jumpCount: 0 + apexTime: 0 --- !u!114 &4623847142764859892 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index 799adf0..33c58d5 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -8,6 +8,10 @@ public class PlayerController : MonoBehaviour { public float jumpHeight = 4f; public float maxForwardJumpBoost = 2.50f; public float timeToJumpApex = 0.4f; + public float lastDashTime = 0f; + public float dashTime = 0.125f; + public float dashSpeed = 50f; + public float dashCooldown = 1.25f; public float moveSpeed = 6f; public float maxFallSpeed = -20f; public float timeToMaxRunSpeed = 0.15f; @@ -16,6 +20,7 @@ public class PlayerController : MonoBehaviour { public float coyoteTime = 0.1f; public int maxJumps = 2; public GameObject tracerPrefab; + public GameObject dashIndicator; // this has to be public to be readable by the display, which is a code // smell. @@ -64,7 +69,9 @@ public class PlayerController : MonoBehaviour { case JumpState.Grounded: tracer.color = Color.white; - if (Input.GetButton("Jump")) { + if (dash()) { + break; + } else if (Input.GetButton("Jump")) { velocity.y = jumpVelocity; if (input.x >= 0.25f) { velocity.x = moveSpeed*maxForwardJumpBoost; @@ -81,6 +88,20 @@ public class PlayerController : MonoBehaviour { } break; + case JumpState.Dash: + tracer.color = Color.red; + velocity.x = Mathf.SmoothDamp(velocity.x, velocity.x >= 0 ? moveSpeed : -moveSpeed, ref velocityXSmoothing, dashTime); + velocity.y = 0; + float timeDashing = Time.time - lastDashTime; + if (timeDashing >= dashTime) { + if (moveController.isGrounded) { + setJumpState(JumpState.Grounded); + } else { + setJumpState(JumpState.Falling); + } + } + break; + case JumpState.Ascending: tracer.color = Color.green; @@ -90,7 +111,9 @@ public class PlayerController : MonoBehaviour { // the drag coefficient gets bigger as we ascend, terminating at 1 float dragCoefficient = n * n; - if (Input.GetButtonDown("Jump") && jumpCount < maxJumps) { + if (dash()) { + break; + } else if (Input.GetButtonDown("Jump") && jumpCount < maxJumps) { velocity.y = jumpVelocity; if (input.x >= 0.25f) { velocity.x = moveSpeed*maxForwardJumpBoost; @@ -150,7 +173,9 @@ public class PlayerController : MonoBehaviour { case JumpState.Apex: tracer.color = Color.magenta; - if (Input.GetButtonDown("Jump") && jumpCount < maxJumps) { + if (dash()) { + break; + } else if (Input.GetButtonDown("Jump") && jumpCount < maxJumps) { velocity.y = jumpVelocity; if (input.x >= 0.25f) { velocity.x = moveSpeed*maxForwardJumpBoost; @@ -222,7 +247,10 @@ public class PlayerController : MonoBehaviour { case JumpState.CoyoteTime: tracer.color = Color.blue; velocity.x = Mathf.SmoothDamp(velocity.x, targetX, ref velocityXSmoothing, timeToMaxRunSpeed); - if (Input.GetButtonDown("Jump")) { + + if (dash()) { + break; + } else if (Input.GetButtonDown("Jump")) { setJumpState(JumpState.Ascending); velocity.y = jumpVelocity; } else { @@ -236,7 +264,7 @@ public class PlayerController : MonoBehaviour { break; case JumpState.Falling: - tracer.color = Color.red; + tracer.color = Color.grey; if (Input.GetButtonDown("Jump") && jumpCount < maxJumps) { velocity.y = jumpVelocity; @@ -292,7 +320,29 @@ public class PlayerController : MonoBehaviour { void OnTriggerEnter(Collider other) { } + bool dash() { + if (!Input.GetButtonDown("Fire1")) { + return false; + } + + if ((Time.time - lastDashTime) < dashCooldown) { + return false; + } + + if (velocity.x >= 0) { + velocity.x = dashSpeed; + } else { + velocity.x = -dashSpeed; + } + velocity.y = 0; + lastDashTime = Time.time; + setJumpState(JumpState.Dash); + + return true; + } + void setJumpState(JumpState state) { + dashIndicator.SetActive(state == JumpState.Dash); if (jumpState != JumpState.Ascending && state == JumpState.Ascending) { jumpStartTime = Time.time; } @@ -319,6 +369,7 @@ public class PlayerController : MonoBehaviour { Possible JumpState transitions: Grounded -> Ascending : a normal jump + Grounded -> Dash : a normal dash Grounded -> CoyoteTime : player has walked off ledge CoyoteTime -> Ascending : player has jumped after leaving a ledge CoyoteTime -> Falling : player has walked off of a ledge and is now falling @@ -348,6 +399,8 @@ public class PlayerController : MonoBehaviour { // The player is descending but without control; they are falling but // did not initially jump. Falling, + + Dash, } public enum JumpDirection {