diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 5e503fa..1978bc2 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -38,7 +38,7 @@ RenderSettings: m_ReflectionIntensity: 1 m_CustomReflection: {fileID: 0} m_Sun: {fileID: 705507994} - m_IndirectSpecularColor: {r: 0.18028326, g: 0.22571333, b: 0.30692202, a: 1} + m_IndirectSpecularColor: {r: 0.18028378, g: 0.22571412, b: 0.30692285, a: 1} m_UseRadianceAmbientProbe: 0 --- !u!157 &3 LightmapSettings: @@ -564,12 +564,19 @@ MonoBehaviour: backwardKey: 111 leftKey: 97 rightKey: 101 - speed: 5 - moveDirection: {x: 0, y: 0, z: 0} - rb: {fileID: 1261174834} + speed: 10 + curSpeed: 0 + dashSpeed: 20 + dashDuration: 0.3 + dashCooldown: 2 + dashKey: 304 + canDash: 1 + dashCooldownTimer: 0 + enemyTag: Enemy invulnerabilityTimeSeconds: 1.92 - lastHit: 0 + lives: 3 metalPipe: {fileID: 932053744} + rb: {fileID: 1261174834} --- !u!65 &1261174830 BoxCollider: m_ObjectHideFlags: 0 @@ -658,7 +665,7 @@ Rigidbody: m_GameObject: {fileID: 1261174828} serializedVersion: 2 m_Mass: 1 - m_Drag: 0 + m_Drag: 2 m_AngularDrag: 0.05 m_UseGravity: 1 m_IsKinematic: 0 diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index b559f6c..8600185 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -1,10 +1,10 @@ using UnityEngine; +using System.Collections; using UnityEngine.UI; #if UNITY_EDITOR using UnityEditor; #endif - public class PlayerController : MonoBehaviour { [Header("Keybinds")] @@ -15,31 +15,31 @@ public class PlayerController : MonoBehaviour [Header("Speed")] public float speed = 5; + public float curSpeed = 0f; + + [Header("Dash")] + public float dashSpeed = 10; + public float dashDuration = 0.3f; // Dash duration + public float dashCooldown = 2f; + public KeyCode dashKey = KeyCode.LeftShift; + public bool canDash = true; + public float dashCooldownTimer = 0f; [Header("Lives")] public string enemyTag = "Enemy"; public float invulnerabilityTimeSeconds = 0.25f; - public float lastHit = 0; public int lives = 3; - public GameObject[] hearts; [Header("Audio")] public AudioSource metalPipe; [Header("Player Object")] - public Vector3 moveDirection; public Rigidbody rb; - + private float lastHit = 0f; + void Start() { - if (hearts.Length != 3) - { - Debug.LogError("Hearts array not properly configured. Expected length 3."); - enabled = false; - return; - } - if (!TryGetComponent(out rb)) { Debug.LogError("Rigidbody not found!"); @@ -53,37 +53,87 @@ public class PlayerController : MonoBehaviour void Update() { - moveDirection = PlayerInput(); - rb.velocity = moveDirection.normalized * speed; + curSpeed = speed; + Vector3 moveDirection = GetPlayerDirection().normalized; + + if (Input.GetKey(dashKey) && canDash) + { + StartCoroutine(DashCooldown()); // Start Dash Coroutine when dash is pressed + curSpeed = dashSpeed; + rb.velocity = new Vector3( + moveDirection.x * curSpeed, + rb.velocity.y, + moveDirection.z * curSpeed + ); + } + else if (Mathf.Pow(rb.velocity.x, 2) + Mathf.Pow(rb.velocity.z, 2) < speed) + { + rb.AddForce(new Vector3 (moveDirection.x * curSpeed, 0, moveDirection.z * curSpeed), ForceMode.Force); + } } - Vector3 PlayerInput() + void HandleDash() { - Vector3 direction = Vector3.zero; - - if (Input.GetKey(rightKey)) direction.x += 1f; - if (Input.GetKey(leftKey)) direction.x -= 1f; - if (Input.GetKey(forwardKey)) direction.z += 1f; - if (Input.GetKey(backwardKey)) direction.z -= 1f; - - return direction; + } + IEnumerator DashCooldown() + { + // Start dash + canDash = false; + + Vector3 dashDirection = GetPlayerDirection().normalized; + rb.velocity = dashDirection * dashSpeed; // Apply dash velocity + + // Wait for the dash to finish + yield return new WaitForSeconds(dashDuration); + + // End dash: reset velocity and start cooldown + rb.velocity = Vector3.zero; // Optionally reset velocity after dash (for more control) + dashCooldownTimer = dashCooldown; + + // Wait for cooldown + yield return new WaitForSeconds(dashCooldown); + + // Allow next dash + canDash = true; + } + + Vector3 GetPlayerDirection() + { + float horizontal = 0f; + float vertical = 0f; + + if (Input.GetKey(rightKey)) horizontal += 1f; + if (Input.GetKey(leftKey)) horizontal -= 1f; + if (Input.GetKey(forwardKey)) vertical += 1f; + if (Input.GetKey(backwardKey)) vertical -= 1f; + + return new Vector3(horizontal, 0, vertical); + } void OnCollisionEnter(Collision collision) { - bool collidedWithEnemy = collision.gameObject.tag == enemyTag; + bool collidedWithEnemy = collision.gameObject.CompareTag(enemyTag); bool isInvulnerable = invulnerabilityTimeSeconds >= Time.time - lastHit; + if (collidedWithEnemy && !isInvulnerable) { - lastHit = Time.time; - metalPipe.Play(); + HandlePlayerHit(); + } + } - lives--; - hearts[lives].GetComponent().color = Color.gray; + void HandlePlayerHit() + { + lastHit = Time.time; + metalPipe.Play(); // Play damage sound effect - bool playerOutOfLives = lives == 0; - if (playerOutOfLives) ExitGame(); + lives--; + // Update visual feedback (if any) for losing a life, e.g. hearts + + if (lives <= 0) + { + ExitGame(); } } @@ -92,6 +142,6 @@ public class PlayerController : MonoBehaviour #if UNITY_EDITOR EditorApplication.ExitPlaymode(); #endif - Application.Quit(); + Application.Quit(); } } diff --git a/Assets/untitled.fbx b/Assets/untitled.fbx new file mode 100644 index 0000000..d058cfe Binary files /dev/null and b/Assets/untitled.fbx differ diff --git a/Assets/untitled.fbx.meta b/Assets/untitled.fbx.meta new file mode 100644 index 0000000..c2c5300 --- /dev/null +++ b/Assets/untitled.fbx.meta @@ -0,0 +1,107 @@ +fileFormatVersion: 2 +guid: 902a72da88f601742b5c96bce99e71a7 +ModelImporter: + serializedVersion: 21300 + internalIDToNameTable: [] + externalObjects: {} + materials: + materialImportMode: 2 + materialName: 0 + materialSearch: 1 + materialLocation: 1 + animations: + legacyGenerateAnimations: 4 + bakeSimulation: 0 + resampleCurves: 1 + optimizeGameObjects: 0 + removeConstantScaleCurves: 1 + motionNodeName: + rigImportErrors: + rigImportWarnings: + animationImportErrors: + animationImportWarnings: + animationRetargetingWarnings: + animationDoRetargetingWarnings: 0 + importAnimatedCustomProperties: 0 + importConstraints: 0 + animationCompression: 1 + animationRotationError: 0.5 + animationPositionError: 0.5 + animationScaleError: 0.5 + animationWrapMode: 0 + extraExposedTransformPaths: [] + extraUserProperties: [] + clipAnimations: [] + isReadable: 0 + meshes: + lODScreenPercentages: [] + globalScale: 1 + meshCompression: 0 + addColliders: 0 + useSRGBMaterialColor: 1 + sortHierarchyByName: 1 + importVisibility: 1 + importBlendShapes: 1 + importCameras: 1 + importLights: 1 + nodeNameCollisionStrategy: 1 + fileIdsGeneration: 2 + swapUVChannels: 0 + generateSecondaryUV: 0 + useFileUnits: 1 + keepQuads: 0 + weldVertices: 1 + bakeAxisConversion: 0 + preserveHierarchy: 0 + skinWeightsMode: 0 + maxBonesPerVertex: 4 + minBoneWeight: 0.001 + optimizeBones: 1 + meshOptimizationFlags: -1 + indexFormat: 0 + secondaryUVAngleDistortion: 8 + secondaryUVAreaDistortion: 15.000001 + secondaryUVHardAngle: 88 + secondaryUVMarginMethod: 1 + secondaryUVMinLightmapResolution: 40 + secondaryUVMinObjectScale: 1 + secondaryUVPackMargin: 4 + useFileScale: 1 + strictVertexDataChecks: 0 + tangentSpace: + normalSmoothAngle: 60 + normalImportMode: 0 + tangentImportMode: 3 + normalCalculationMode: 4 + legacyComputeAllNormalsFromSmoothingGroupsWhenMeshHasBlendShapes: 0 + blendShapeNormalImportMode: 1 + normalSmoothingSource: 0 + referencedClips: [] + importAnimation: 1 + humanDescription: + serializedVersion: 3 + human: [] + skeleton: [] + armTwist: 0.5 + foreArmTwist: 0.5 + upperLegTwist: 0.5 + legTwist: 0.5 + armStretch: 0.05 + legStretch: 0.05 + feetSpacing: 0 + globalScale: 1 + rootMotionBoneName: + hasTranslationDoF: 0 + hasExtraRoot: 0 + skeletonHasParents: 1 + lastHumanDescriptionAvatarSource: {instanceID: 0} + autoGenerateAvatarMappingIfUnspecified: 1 + animationType: 2 + humanoidOversampling: 1 + avatarSetup: 0 + addHumanoidExtraRootOnlyWhenUsingAvatar: 1 + remapMaterialsIfMaterialImportModeIsNone: 0 + additionalBone: 0 + userData: + assetBundleName: + assetBundleVariant: