protection against getting carried off after being hit

This commit is contained in:
Arlo Filley 2024-11-24 17:46:56 +00:00
parent 7ca2bd0b30
commit 2b99cb9579
4 changed files with 60 additions and 26 deletions

View File

@ -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()
{
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3af27df6293e26441a12156d57c150b6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -6,7 +6,7 @@ public class EnemySpawner : MonoBehaviour
public GameObject enemyPrefab; // Reference to the enemy prefab public GameObject enemyPrefab; // Reference to the enemy prefab
public float spawnInterval = 3f; // Time between each spawn (in seconds) public float spawnInterval = 3f; // Time between each spawn (in seconds)
public float enemyLifetime = 5f; // Time before the enemy is destroyed (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() void Start()
{ {

View File

@ -13,36 +13,34 @@ public class PlayerController : MonoBehaviour
public KeyCode backwardKey = KeyCode.S; public KeyCode backwardKey = KeyCode.S;
public KeyCode leftKey = KeyCode.A; public KeyCode leftKey = KeyCode.A;
public KeyCode rightKey = KeyCode.D; public KeyCode rightKey = KeyCode.D;
public KeyCode dashKey = KeyCode.LeftShift;
[Header("Speed")] [Header("Speed")]
public float speed = 5; public float speed = 5;
public float curSpeed = 0f;
public bool canMove = true; public bool canMove = true;
[Header("Dash")] [Header("Dash")]
public float dashSpeed = 10; public float dashSpeed = 10;
public float dashDuration = 0.3f; // Dash duration public float dashDuration = 0.3f; // Dash duration
public float dashCooldown = 2f; public float dashCooldown = 2f;
public KeyCode dashKey = KeyCode.LeftShift;
public bool canDash = true; public bool canDash = true;
public float dashCooldownTimer = 0f; public ParticleSystem dashParticles;
public ParticleSystem particleSystem; public Transform dashParticlesTransform;
public Transform particleTransform;
[Header("Lives")] [Header("Lives")]
public string enemyTag = "Enemy"; public string enemyTag = "Enemy";
public float invulnerabilityTimeSeconds = 0.25f; public float invulnerabilityTime = 0.25f;
public bool isInvulnerable = false;
public int lives = 3; public int lives = 3;
public ParticleSystem bloodParticleSystem; public ParticleSystem bloodParticles;
public AudioSource damageSound;
[Header("Audio")]
public AudioSource metalPipe;
[Header("Player Object")] [Header("Player Object")]
public Rigidbody rb; public Rigidbody rb;
public BoxCollider collision;
private float lastHit = 0f;
void Start() void Start()
{ {
@ -59,7 +57,6 @@ public class PlayerController : MonoBehaviour
void Update() void Update()
{ {
curSpeed = speed;
Vector3 moveDirection = GetPlayerDirection().normalized; Vector3 moveDirection = GetPlayerDirection().normalized;
if (Input.GetKey(dashKey) && canDash) if (Input.GetKey(dashKey) && canDash)
@ -69,9 +66,9 @@ public class PlayerController : MonoBehaviour
else if (canMove) else if (canMove)
{ {
rb.velocity = new Vector3( rb.velocity = new Vector3(
moveDirection.x * curSpeed, moveDirection.x * speed,
rb.velocity.y, rb.velocity.y,
moveDirection.z * curSpeed moveDirection.z * speed
); );
} }
} }
@ -83,12 +80,14 @@ public class PlayerController : MonoBehaviour
Vector3 dashDirection = GetPlayerDirection().normalized; Vector3 dashDirection = GetPlayerDirection().normalized;
rb.velocity = dashDirection * dashSpeed; rb.velocity = dashDirection * dashSpeed;
particleSystem.Play(); dashParticles.Play();
particleTransform.SetLocalPositionAndRotation(dashDirection * -1.5f, particleTransform.localRotation); // 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); yield return new WaitForSeconds(dashDuration);
particleSystem.Stop(); dashParticles.Stop();
rb.velocity = Vector3.zero; rb.velocity = Vector3.zero;
canMove = true; canMove = true;
@ -114,15 +113,14 @@ public class PlayerController : MonoBehaviour
void OnCollisionEnter(Collision collision) void OnCollisionEnter(Collision collision)
{ {
bool collidedWithEnemy = collision.gameObject.CompareTag(enemyTag); bool collidedWithEnemy = collision.gameObject.CompareTag(enemyTag);
bool isInvulnerable = invulnerabilityTimeSeconds >= Time.time - lastHit;
if (collidedWithEnemy && !isInvulnerable) if (collidedWithEnemy && !isInvulnerable)
{ {
HandlePlayerHit(); StartCoroutine(Hit());
} }
} }
void HandlePlayerHit() IEnumerator Hit()
{ {
lives--; lives--;
if (lives <= 0) if (lives <= 0)
@ -130,13 +128,20 @@ public class PlayerController : MonoBehaviour
ExitGame(); ExitGame();
} }
bloodParticleSystem.Play(); bloodParticles.Play();
lastHit = Time.time; damageSound.Play();
metalPipe.Play(); // Play damage sound effect
rb.isKinematic = true;
isInvulnerable = true;
// Update visual feedback (if any) for losing a life, e.g. hearts yield return new WaitForSeconds(invulnerabilityTime / 2);
rb.isKinematic = false;
yield return new WaitForSeconds(invulnerabilityTime / 2);
isInvulnerable = false;
} }
void ExitGame() void ExitGame()