I’m quite a beginner in scripting. And I can’t find anything on google that helps. I want to create a random movement with the AI, this is what I have put together so far, but the enemy does not move with this.
What should be done differently?
{
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Mushroom_Dmg : MonoBehaviour
{
GameObject player;
NavMeshAgent agent;
[SerializeField] LayerMask groundLayer, playerLayer;
//patrol
Vector3 desPoint;
bool walkpointSet;
[SerializeField] float range;
//state change
[SerializeField] Vector3 sightRange, attackRange;
bool playerInSight, playerInAttackRange;
void Start()
{
agent = GetComponent<NavMeshAgent>();
player = GameObject.Find("Player");
}
void Update()
{
playerInSight = Physics.CheckCapsule(transform.position, sightRange, playerLayer);
playerInAttackRange = Physics.CheckCapsule(transform.position, attackRange, playerLayer);
if (!playerInSight && !playerInAttackRange) Patrol();
if (playerInSight && !playerInAttackRange) Chase();
if (playerInSight && playerInAttackRange) Attack();
}
void Chase()
{
agent.SetDestination(player.transform.position);
}
void Attack()
{
}
void Patrol()
{
if (!walkpointSet) SearchForDest() ;
if (walkpointSet) agent.SetDestination(desPoint);
if (Vector3.Distance(transform.position, desPoint) < 10) walkpointSet = false;
}
void SearchForDest()
{
float z = Random.Range(-range, range);
float x = Random.Range(-range, range);
desPoint = new Vector3(transform.position.x + x, transform.position.y, transform.position.z + z);
if (Physics.Raycast(desPoint, Vector3.down, groundLayer))
{
walkpointSet = true;
}
}
}
}