Hey all, I’m having trouble getting an enemy patrol function to work. In a nutshell, the enemy will move between two points. If the player hits a particular trigger, the enemy abandons patrolling those two points and pursues player.
Unfortunately, I can’t seem to get my method to work. The enemy just stands still. I can get the enemy to pursue the player when I comment out the method and just put “meshAgent.SetDestination(playerMovement.transform.position);” in the Awake function.
Can any of you identify the issue with my Patrol method? Here are the relevant scripts (they’re a bit of a mess, as I’m mainly experimenting).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.AI;
public class Enemy : MonoBehaviour
{
int currentPoint;
public Transform[] patrolPoint;
float movementSpeed = 5f;
PlayerMovement playerMovement;
public NavMeshAgent meshAgent;
void Awake ()
{
transform.position = patrolPoint[0].position;
currentPoint = 0;
playerMovement = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMovement>();
meshAgent = GetComponent<NavMeshAgent>();
}
void Update ()
{
Patrol();
}
private void Patrol()
{
if (playerMovement.enemyTrigger == true)
{
meshAgent.SetDestination(playerMovement.transform.position);
}
else
{
if (transform.position == patrolPoint[currentPoint].position)
{ currentPoint++; }
if (currentPoint >= patrolPoint.Length)
{
currentPoint = 0;
}
transform.position = Vector3.MoveTowards(transform.position, patrolPoint[currentPoint].position, movementSpeed * Time.deltaTime);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public float moveSpeed;
float maxSpeed = 5;
Rigidbody rb;
Vector3 input;
public Vector3 spawnPoint;
public GameObject deathParticles;
MeshRenderer meshRenderer;
Enemy enemy;
public bool isDead = false;
public bool enemyTrigger = false;
void Start ()
{
rb = GetComponent<Rigidbody>();
spawnPoint = transform.position;
meshRenderer = GetComponent<MeshRenderer>();
enemy = GameObject.FindGameObjectWithTag("Enemy").GetComponent<Enemy>();
}
void FixedUpdate ()
{
input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
if(rb.velocity.magnitude < maxSpeed)
{
rb.AddForce(input * moveSpeed, ForceMode.Acceleration);
}
FallDeath();
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Enemy")
{
Die();
}
if (other.tag == "EnemyTrigger")
{
enemyTrigger = true;
}
}
private void OnCollisionEnter(Collision collision)
{
if(collision.collider.tag == "Trap")
{
Die();
}
}
public void Die()
{
Instantiate(deathParticles, transform.position, Quaternion.identity);
meshRenderer.enabled = false;
rb.isKinematic = true;
isDead = true;
transform.position = spawnPoint;
StartCoroutine (Wait());
}
void FallDeath()
{
if (transform.position.y <= 0.9f)
{
Die();
}
}
public IEnumerator Wait()
{
if (isDead)
{
yield return new WaitForSeconds(2);
rb.isKinematic = false;
isDead = false;
meshRenderer.enabled = true;
}
}
}
I tried using a while loop with the enemyTrigger bool, but that caused Unity to freeze. I appreciate any help you can provide.