Gameplay.MimicEnemy - robblofield/TomeboundDocs GitHub Wiki
Gameplay.MimicEnemy
Author: Shahrzad Beevis
Last Updated: 16/02/2024
Overview
A script that controls a 'mimic enemy', making the GameObject it is attached to mimic and mirror the players movements based on movement direction.
Dependencies
N/A
Contents
- Use case
- Breakdown of code
- Using Statements
- Class Declaration
- Variables
- Method/Start()
- Method/Update()
- Future Expansion
- Full Code Reference
Use case
This script can be employed to create a challenging enemy that mimics the movements of two player characters.
Breakdown of Code
Using Statements
using UnityEngine;
Class Declaration
public class MimicEnemy : MonoBehaviour
Variables
[SerializeField] private GameObject playerWizardY;
[SerializeField] private GameObject playerWizardX;
private Transform playerTransform;
private bool isFollowing = true;
private Vector3 previousPlayerPosition;
public float movementSpeed = 5f;
[SerializeField] private GameObject playerWizardY & [SerializeField] private GameObject playerWizardX:
These lines declare two private GameObject variables, playerWizardY and playerWizardX - both of which are editable within the inspector where the relevant GameObjects will be applied.
private Transform playerTransform:
This variable will be used to store the Transform component of the player GameObject that the MimicEnemy is currently following.
private bool isFollowing = true:
Declares a private boolean variable isFollowing and initialises it to true. This variable is used to control whether the enemy is actively following the player or not.
private Vector3 previousPlayerPosition:
Declares a private variable previousPlayerPosition of type Vector3. This variable will store the position of the player in the previous frame. It is used to calculate the movement direction of the player, enabling the enemy to mimic the player's movements.
public float movementSpeed = 5f:
Declares a public float variable movementSpeed and initializes it to 5f. This variable determines the speed at which the enemy mimics the player's movement and is editable within the inspector.
Start()
void Start()
{
if (playerWizardY == null || playerWizardX == null)
{
Debug.LogError("Player GameObjects not assigned to MimicEnemy script");
return;
}
playerTransform = playerWizardY.transform;
previousPlayerPosition = playerTransform.position;
}}
This method is executed once when the script starts. It checks if both player GameObjects are assigned. If they are not, it logs an error to the console and exits the Start method. It then sets playerTransform to the Transform component of playerWizardY and initialises previousPlayerPosition to the starting position of the selected player.
Update()
{
if (isFollowing)
{
Vector3 movementDirection = playerTransform.position - previousPlayerPosition;
movementDirection.z = -movementDirection.z;
Vector3 targetPosition = transform.position + movementDirection;
transform.position = Vector3.MoveTowards(transform.position, targetPosition, Time.deltaTime * movementSpeed);
previousPlayerPosition = playerTransform.position;
}
if (playerWizardY.GetComponent<GridBasedMovement1>().isMoving)
{
playerTransform = playerWizardY.transform;
}
else if (playerWizardX.GetComponent<GridBasedMovement1>().isMoving)
{
playerTransform = playerWizardX.transform;
}
}
This method is executed every frame. It starts by checking if isFollowing is true, if so it then calculates the movement direction based on the change in the player's position. The method also inverts the movement on the z-axis allowing the enemy to move closer to you as you move through the map. It then mimics the player's movement by updating the enemy's position using Vector3.MoveTowards and updates previousPlayerPosition for the next frame. Following this it checks if either player is currently moving (isMoving is part of the GridBasedMovement1 script) - If playerWizardY is moving, it updates playerTransform to target playerWizardY. If playerWizardX is moving, it updates playerTransform to target playerWizardX.
Future Expansion
N/A
Full Code Reference
using UnityEngine;
public class MimicEnemy : MonoBehaviour
{
[SerializeField] private GameObject playerWizardY;
[SerializeField] private GameObject playerWizardX;
private Transform playerTransform;
private bool isFollowing = true;
private Vector3 previousPlayerPosition;
public float movementSpeed = 5f;
void Start()
{
if (playerWizardY == null || playerWizardX == null)
{
Debug.LogError("Player GameObjects not assigned to MimicEnemy script");
return;
}
playerTransform = playerWizardY.transform;
previousPlayerPosition = playerTransform.position;
}
void Update()
{
if (isFollowing)
{
Vector3 movementDirection = playerTransform.position - previousPlayerPosition;
movementDirection.z = -movementDirection.z;
Vector3 targetPosition = transform.position + movementDirection;
transform.position = Vector3.MoveTowards(transform.position, targetPosition, Time.deltaTime * movementSpeed);
previousPlayerPosition = playerTransform.position;
}
if (playerWizardY.GetComponent<GridBasedMovement1>().isMoving)
{
playerTransform = playerWizardY.transform;
}
else if (playerWizardX.GetComponent<GridBasedMovement1>().isMoving)
{
playerTransform = playerWizardX.transform;
}
}
}