参考博文
https://blog.csdn.net/c252270036/article/details/77414141
因为做的是即时战斗,故在原博文的基础上进行了些修改
1.原博文中,怪物的攻击方式是切换场景,这里添加了攻击间隔。
2.添加了注视时间,当进入警戒状态并维持一定时间后,怪物会主动发起追击。
3.修改了追击模式下的移动判断。
效果如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MonsterAI : MonoBehaviour
{
public GameObject playerUnit;
public ActorController ac;
public Animator anim;
private Vector3 initialPosition;
public float wanderRadius;
public float alertRadius;
public float defendRadius;
public float chaseRadius;
public float attackRange;
public float walkSpeed;
public float runSpeed;
public float turnSpeed;
private enum MonsterState
{
STAND,
CHECK,
WALK,
WARN,
CHASE,
RETURN
}
private MonsterState currentState = MonsterState.STAND;
public float[] actionWeight = {
3000, 3000, 4000 };
public float actRestTime;
public float atkRestTime; //攻击间隔
public float warnRestTime; //注视时间,当进入警戒状态并维持一定时间后,怪物会主动发起追击。
private float lastActTime;
private float lastAtkTime;
private float distanceToPlayer;
private float distanceToInitial;
private Quaternion targetRotation;
private bool is_warned = false;
private bool is_Running = false;
void Start()
{
playerUnit = GameObject.FindGameObjectWithTag("Player");
ac = GetComponent<ActorController>();
anim = ac.model.GetComponent<Animator>(