I have a game where the player needs to avoid a monster. If the player comes close the monster will chase the player. But if the player does not come in range, he just stands still. I find that boring, so I wanted to make him move around. The full script for the enemy is this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Walker : MonoBehaviour
{
public float wanderTimer = 5f;
private float timer;
public float lookRadius = 10f;
NavMeshAgent agent;
Transform target;
public bool isChasingPlayer;
void Start()
{
target = PlayerManager.instance.player.transform;
agent = GetComponent<NavMeshAgent>();
timer = wanderTimer;
}
void Update()
{
float distance = Vector3.Distance(target.position, transform.position);
if (distance <= lookRadius)
{
agent.SetDestination(target.position);
isChasingPlayer = true;
}
else
{
isChasingPlayer = false;
}
if(isChasingPlayer == false)
{
timer += Time.deltaTime;
if(timer >= wanderTimer)
{
//What do I need to put here?
agent.SetDestination();
timer = 0;
}
}
else
{
timer = 0;
}
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, lookRadius);
}
}
At the comment “What do I put here” I should add a vector3 that will be a destination for the agent. I’m a new programmer and have no idea how I could do that.