Functionality and Importance
Functionality and Importance
1. EnemyFollow.cs
Purpose: This script controls the behavior of an enemy that can follow and
attack the player.
Key Components:
Movement: The enemy has an initial speed that decreases over time
until it reaches a minimum speed. If the player is within a detection
range, the enemy follows the player. If the player is within an attack
range, the enemy will attack instead.
Attack Mechanism: The enemy will deal damage to the player when
within attack range.
2. Exit.cs
Purpose: Manages the exit point for the player to transition to the next
level.
Key Components:
Trigger Detection: The script checks for collisions with the player
using a trigger collider.
3. MainMenu.cs
Key Components:
Play Game: Loads the first game scene when the player chooses to
start the game.
Quit Game: Logs a message to the console and closes the application.
Importance: This script is essential for managing user input from the main
menu, providing a starting point and the ability to quit the game.
4. PauseMenu.cs
Key Components:
Resume: Hides the pause menu and resumes the game by resetting
Time.timeScale.
Restart: Allows the player to restart the game by loading the main
menu scene.
5. PlayerController.cs
Purpose: Manages the player's movement, health, and interactions with the
game world.
Key Components:
6. TrapController.cs
Key Components:
Summary
Each of these scripts plays a vital role in the overall game functionality:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public float minSpeed = 0.5f; // Minimum speed the enemy can reach
public float minVolume = 0.1f; // Minimum volume when far from the
player
void Start()
if (player != null)
target = player.transform;
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
currentSpeed = initialSpeed;
SetRoamPosition();
if (enemyAudioSource == null)
enemyAudioSource = GetComponent<AudioSource>();
}
void Update()
if (target != null)
UpdateAudioVolume(distanceToTarget);
if (!isAttacking)
Attack();
FollowTarget();
}
else
// Roam around
Roam();
elapsedTime += Time.deltaTime;
if (animator != null)
animator.SetFloat("Speed", rb.velocity.magnitude);
}
private void Attack()
isAttacking = true;
if (animator != null)
animator.SetTrigger("Attack");
PlayerController playerHealth =
target.GetComponent<PlayerController>();
if (playerHealth != null)
playerHealth.TakeDamage(damage);
SetRoamPosition();
}
if (animator != null)
animator.SetFloat("Speed", rb.velocity.magnitude);
if (enemyAudioSource != null)
{
enemyAudioSource.volume = volume;
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
private bool canExit = false; // Flag to check if the player can exit
StartCoroutine(NextLevel());
// Method to set the flag when the player interacts with the NPC
public void AllowExit()
canExit = true;
Time.timeScale = slowMotion;
Time.timeScale = 1f;
SceneManager.LoadScene(currentSceneIndex + 1);
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
SceneManager.LoadSceneAsync(1);
}
Debug.Log ("QUIT!");
Application.Quit();
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
// UI elements
void Start()
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
spriteRenderer = GetComponent<SpriteRenderer>();
if (deathSound == null)
// Add listeners
returnToLobbyButton.onClick.AddListener(ReturnToLobby);
continueButton.onClick.AddListener(ContinueToNextScene);
if (movementInput != Vector2.zero)
TryMove(movementInput);
if (isFading)
if (direction != Vector2.zero)
{
if (count == 0)
return true;
return false;
TakeDamage(damageFromEnemy);
{
if (isDead) return; // Prevent further damage if already dead
Die();
animator.SetTrigger("Transform");
flashUI?.Flash();
flashUI?.LoseLife();
deathSound?.Play();
if (lives <= 0)
StartCoroutine(HandleGameOverScreen());
else
StartCoroutine(HandleBlackScreen());
StartCoroutine(RemoveAfterAnimation());
}
if (remainingLivesText != null)
ToggleEnemies(false);
}
private void ContinueToNextScene()
lives = 3;
UpdateLivesText();
gameOverText.enabled = false;
returnToLobbyButton.gameObject.SetActive(false);
continueButton.gameObject.SetActive(false);
StartCoroutine(HandleBlackScreenAfterGameOver());
isFading = true;
StartCoroutine(RemoveAfterAnimation());
canMove = true;
if (lives > 0)
GameObject[] enemies =
GameObject.FindGameObjectsWithTag("Enemy");
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public float damageInterval = 1f; // How often the trap deals damage
void Update()
if (playerInRange)
damageTimer += Time.deltaTime;
damageTimer = 0f;
DealDamageToPlayer();
}
}
if (player != null)
PlayerController playerController =
player.GetComponent<PlayerController>();
if (playerController != null)
playerController.TakeDamage(trapDamage);
if (collider.CompareTag("Player"))
playerInRange = true;
if (collider.CompareTag("Player"))
playerInRange = false;
SceneManager.LoadScene(newSettingB);