From 2b99cb95799604f00d0ddbfd48cff985fd9d5589 Mon Sep 17 00:00:00 2001 From: Arlo Filley <104267514+ArloFilley@users.noreply.github.com> Date: Sun, 24 Nov 2024 17:46:56 +0000 Subject: [PATCH] protection against getting carried off after being hit --- Assets/Scripts/CameraController.cs | 18 ++++++++ Assets/Scripts/CameraController.cs.meta | 11 +++++ Assets/Scripts/EnemySpawner.cs | 2 +- Assets/Scripts/PlayerController.cs | 55 ++++++++++++++----------- 4 files changed, 60 insertions(+), 26 deletions(-) create mode 100644 Assets/Scripts/CameraController.cs create mode 100644 Assets/Scripts/CameraController.cs.meta diff --git a/Assets/Scripts/CameraController.cs b/Assets/Scripts/CameraController.cs new file mode 100644 index 0000000..a58c9ff --- /dev/null +++ b/Assets/Scripts/CameraController.cs @@ -0,0 +1,18 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class CameraController : MonoBehaviour +{ + // Start is called before the first frame update + void Start() + { + + } + + // Update is called once per frame + void Update() + { + + } +} diff --git a/Assets/Scripts/CameraController.cs.meta b/Assets/Scripts/CameraController.cs.meta new file mode 100644 index 0000000..6a1bf4b --- /dev/null +++ b/Assets/Scripts/CameraController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3af27df6293e26441a12156d57c150b6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/EnemySpawner.cs b/Assets/Scripts/EnemySpawner.cs index faae24b..b04ba64 100644 --- a/Assets/Scripts/EnemySpawner.cs +++ b/Assets/Scripts/EnemySpawner.cs @@ -6,7 +6,7 @@ public class EnemySpawner : MonoBehaviour public GameObject enemyPrefab; // Reference to the enemy prefab public float spawnInterval = 3f; // Time between each spawn (in seconds) public float enemyLifetime = 5f; // Time before the enemy is destroyed (in seconds) - public Vector3 spawnPosition = new Vector3(0f, 0.6f, 0f); // Position to spawn enemies + public Vector3 spawnPosition = new Vector3(50f, 0.6f, 50f); // Position to spawn enemies void Start() { diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index a642c0d..a3158e0 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -13,36 +13,34 @@ public class PlayerController : MonoBehaviour public KeyCode backwardKey = KeyCode.S; public KeyCode leftKey = KeyCode.A; public KeyCode rightKey = KeyCode.D; + public KeyCode dashKey = KeyCode.LeftShift; [Header("Speed")] public float speed = 5; - public float curSpeed = 0f; public bool canMove = true; [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; - public ParticleSystem particleSystem; - public Transform particleTransform; + public ParticleSystem dashParticles; + public Transform dashParticlesTransform; [Header("Lives")] public string enemyTag = "Enemy"; - public float invulnerabilityTimeSeconds = 0.25f; + public float invulnerabilityTime = 0.25f; + public bool isInvulnerable = false; public int lives = 3; - public ParticleSystem bloodParticleSystem; - - [Header("Audio")] - public AudioSource metalPipe; + public ParticleSystem bloodParticles; + public AudioSource damageSound; [Header("Player Object")] public Rigidbody rb; + public BoxCollider collision; - private float lastHit = 0f; + void Start() { @@ -59,7 +57,6 @@ public class PlayerController : MonoBehaviour void Update() { - curSpeed = speed; Vector3 moveDirection = GetPlayerDirection().normalized; if (Input.GetKey(dashKey) && canDash) @@ -69,9 +66,9 @@ public class PlayerController : MonoBehaviour else if (canMove) { rb.velocity = new Vector3( - moveDirection.x * curSpeed, + moveDirection.x * speed, rb.velocity.y, - moveDirection.z * curSpeed + moveDirection.z * speed ); } } @@ -83,12 +80,14 @@ public class PlayerController : MonoBehaviour Vector3 dashDirection = GetPlayerDirection().normalized; rb.velocity = dashDirection * dashSpeed; - particleSystem.Play(); - particleTransform.SetLocalPositionAndRotation(dashDirection * -1.5f, particleTransform.localRotation); + dashParticles.Play(); + // Positions the dash particle system behind the player so that they + // follow the player in a nice way + dashParticlesTransform.SetLocalPositionAndRotation(dashDirection * -1.5f, dashParticlesTransform.localRotation); yield return new WaitForSeconds(dashDuration); - particleSystem.Stop(); + dashParticles.Stop(); rb.velocity = Vector3.zero; canMove = true; @@ -114,15 +113,14 @@ public class PlayerController : MonoBehaviour void OnCollisionEnter(Collision collision) { bool collidedWithEnemy = collision.gameObject.CompareTag(enemyTag); - bool isInvulnerable = invulnerabilityTimeSeconds >= Time.time - lastHit; if (collidedWithEnemy && !isInvulnerable) { - HandlePlayerHit(); + StartCoroutine(Hit()); } } - void HandlePlayerHit() + IEnumerator Hit() { lives--; if (lives <= 0) @@ -130,13 +128,20 @@ public class PlayerController : MonoBehaviour ExitGame(); } - bloodParticleSystem.Play(); + bloodParticles.Play(); - lastHit = Time.time; - metalPipe.Play(); // Play damage sound effect + damageSound.Play(); - - // Update visual feedback (if any) for losing a life, e.g. hearts + rb.isKinematic = true; + isInvulnerable = true; + + yield return new WaitForSeconds(invulnerabilityTime / 2); + + rb.isKinematic = false; + + yield return new WaitForSeconds(invulnerabilityTime / 2); + + isInvulnerable = false; } void ExitGame()