Hi! I’m trying to make an object die when the player approaches it. Any tips on what I’m doing wrong?
Script for the object (It patrols around)
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class NPCPatrol : MonoBehaviour
{
//Determines whether NPC waits on a node
[SerializeField]
bool _patrolWaiting;
//Sight distance
[SerializeField]
public float sightDistance = 10f;
//Total time to wait on a node
[SerializeField]
float _totalWaitTime = 3f;
//Probability to switch directions
[SerializeField]
float _switchProbability = 0.2f;
//List of all patrol points to go to
[SerializeField]
List<PatrolPoint> _patrolPoints;
//Private variables for behavior
NavMeshAgent _navMeshAgent;
int _currentPatrolIndex;
bool _travelling;
bool _waiting;
bool _patrolForward;
float _waitTimer;
Transform target;
Transform pad;
// Start is called before the first frame update
void Start()
{
target = PlayerTracker.instance.player.transform; //Calls on GameManager's PlayerTracker
pad = PlayerTracker.instance.pad.transform;
_navMeshAgent = this.GetComponent<NavMeshAgent>();
if(_navMeshAgent == null) //If true no nav mesh attached
{
Debug.LogError("Nav mesh agent is not attached to " + gameObject.name);
}
else
{
if(_patrolPoints != null && _patrolPoints.Count >= 2) //Checks if patrol points exist and there are enough
{
_currentPatrolIndex = 0;
SetDestination();
}
else
{
Debug.Log("Insufficient patrol points for basic patrolling behaviour.");
}
}
}
// Update is called once per frame
public void Update()
{
float distance = Vector3.Distance(target.position, transform.position);
//Checks if close to the destination
if (_travelling && _navMeshAgent.remainingDistance <= 1.0f)
{
_travelling = false;
//If going to wait
if (_patrolWaiting)
{
_waiting = true;
_waitTimer = 0f;
}
else
{
ChangePatrolPoint();
SetDestination();
}
}
//Instead if we're waiting
if (_waiting)
{
_waitTimer += Time.deltaTime;
if(_waitTimer >= _totalWaitTime)
{
_waiting = false;
ChangePatrolPoint();
SetDestination();
}
}
if (distance <= sightDistance)
{
gameObject.SetActive(false);
}
}
private void SetDestination() //Sets destination
{
if(_patrolPoints != null)
{
Vector3 targetVector = _patrolPoints[_currentPatrolIndex].transform.position; //Go to destination
_navMeshAgent.SetDestination(targetVector);
_travelling = true;
}
}
private void ChangePatrolPoint()
{
if(UnityEngine.Random.Range(0f, 1f) <= _switchProbability)
{
_patrolForward = !_patrolForward;
}
if (_patrolForward)
{
_currentPatrolIndex = (_currentPatrolIndex + 1) % _patrolPoints.Count;
}
else
{
if(--_currentPatrolIndex < 0)
{
_currentPatrolIndex = _patrolPoints.Count - 1;
}
}
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.blue; //Makes sight radius sphere blue
Gizmos.DrawWireSphere(transform.position, sightDistance); //shows radius of sight
}
}
Script for the GameManager that tracks the player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerTracker : MonoBehaviour
{
#region Singleton
public static PlayerTracker instance;
void Awake()
{
instance = this;
}
#endregion
public GameObject player; //References player
public GameObject pad;
}
If you need anything else to help solve the problem let me know