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

View File

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