I’ve created a player and an enemy. The enemy chases the player and deals damage when it comes into contact with the player. That part’s fine for now, however I’ve also implemented a start screen where you have to select ‘Start’ to get onto the game screen.
The problem is that the enemy starts to chase the player as soon as the game runs and doesn’t wait until play is selected.
Is there some easy way to get the enemy to wait until the scene is changed to the game one? Probably a very stupid question but I’ve only just started.
Also sorry for the obnoxious commenting but it’s necessary because this is a school project.
Player Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f; // How fast the player moves
public Rigidbody2D rb; // Referencing Rigidbody to allow the player to move. This is needed because it is Unity's base motot that allows the player to move and understands the code written to translate it in game
public Weapon weapon; // Referencing the Weapon script so that we can access it from this code
Vector2 moveDirection; // Where the player is moving on the screen
Vector2 mousePosition; // The position of the cursor on the screen
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update() // Called once per frame so will handle all of the inputs from the user
{
float moveX = Input.GetAxisRaw("Horizontal");
float moveY = Input.GetAxisRaw("Vertical"); // Taking the x and y inputs from the user
moveDirection = new Vector2 (moveX, moveY).normalized; // Assigning the x and y movement of the player to the variable to use in calculations. '.normalized' is needed so that the player doesn't go faster when moving in a diagonal line
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); // Assigning the position of the cursor on the screen to use in calculations
if (Input.GetButtonDown("Fire1")) // If the user presses any of the buttons bound to 'Fire1'
{
weapon.Fire(); // Referencing the fire method in the weapon script
}
}
void FixedUpdate() // Called at fixed intervals, so it will handle the movement because we want all the values to be called at constant intervals to get the correct calculations
{
rb.velocity = new Vector2(moveDirection.x * moveSpeed, moveDirection.y * moveSpeed); // Calculate where the player needs to move based on the inputs of the user multiplied by the move speed that was chosen
Vector2 aimDirection = mousePosition - rb.position; // Calculate the direction that the player is aiming in by taking away the coordinates of the player from the coordinates of the cursor
float aimAngle = Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg - 90f; // This is the calculation to find out how much we need to rotate the player in order for them to be looking at the cursor
rb.rotation = aimAngle; // Rotating the player the necessary amount to be looking at the cursor
}
}
Enemy script:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
[SerializeField] float health, maxHealth = 3f; // This creates two variables, health and max health. SerializeField is needed so that these variables can stay private but be accessible within the Unity editor
[SerializeField] float moveSpeed = 2f;
Rigidbody2D rb;
Transform target;
Vector2 moveDirection;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
private void Start()
{
health = maxHealth; // This sets the health to the maximum health at the start of the game
target = GameObject.Find("Player").transform;
}
private void Update()
{
if(target)
{
Vector3 direction = (target.position- transform.position).normalized;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
rb.rotation = angle;
moveDirection = direction;
}
}
private void FixedUpdate()
{
if(target)
{
rb.velocity = new Vector2(moveDirection.x, moveDirection.y) * moveSpeed;
}
}
public void TakeDamage(float damageAmount) // Method that takes in a damage amount as a float and inflicts it upon the enemy
{
health -= damageAmount; // The damage amount is taken away from the current health
if (health <= 0 ) // If the enemy has not more health left once it has taken damage:
{
Destroy(gameObject); // Then it dies
}
}
}
Menu script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement; // Allows me to change the current scene (for the play button)
public class MainMenu : MonoBehaviour
{
public void PlayGame () // Method to run when 'PLAY' is selected on the menu screen
{
SceneManager.LoadScene(1); // Increments the active scene by 1 in order to get to the game screen
}
public void QuitGame() // Method to run when 'QUIT' is selected
{
Debug.Log("QUIT"); // This is purely for my use, since you cannot tell when the game has been quit inside if the unity editor
Application.Quit(); // Quits the game
}
}