更改总则
1.添加退出battleState的时间
在Enemy.cs中定义变量battleTime
[Header("Move info")]
public float moveSpeed;
public float idleTime;
public float battleTime;
然后在BattleState.cs的Update中添加
stateTimer = enemy.battleTime;
if(enemy.IsPlayerDetected())
{
stateTimer = enemy.battleTime;
if(enemy.IsPlayerDetected().distance<enemy.attackDistance)
{
if(CanAttack())
stateMachine.ChangeState(enemy.attackState);
}
}
else
{
if(stateTimer < 0||Vector2.Distance(player.transform.position,enemy.transform.position)>7)
{
stateMachine.ChangeState(enemy.idleState);
}
}
2.添加退出battleState的距离
if(stateTimer < 0||Vector2.Distance(player.transform.position,enemy.transform.position)>7)
{
stateMachine.ChangeState(enemy.idleState);
}
3.使敌人能够发现身后的敌人
在GroundState中定义变量protected Transform player;
if(enemy.IsPlayerDetected()||Vector2.Distance(enemy.transform.position,
player.transform.position)<2)
{
stateMachine.ChangeState(enemy.battleState);
}
更改后总代码
Enemy.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : Entity
{
[SerializeField] protected LayerMask whatIsPlayer;
[Header("Move info")]
public float moveSpeed;
public float idleTime;
public float battleTime;
[Header("Attack info")]
public float attackDistance;
public float attackCooldown;
[HideInInspector] public float lastTimeAttacked;
public EnemyStateMachine stateMachine { get; private set; }//定义敌人状态机类
//唤醒控制器
protected override void Awake()
{
base.Awake();
stateMachine = new EnemyStateMachine();
}
protected override void Update()
{
base.Update();
stateMachine.currentState.Update();
}
public virtual void AnimationFinishTrigger() => stateMachine.currentState.AnimationFinishTrigger();
public virtual RaycastHit2D IsPlayerDetected() => Physics2D.Raycast(wallCheck.position, Vector2.right * facingDir, 10, whatIsPlayer);
protected override void OnDrawGizmos()
{
base.OnDrawGizmos();
Gizmos.color = Color.yellow;
Gizmos.DrawLine(transform.position, new Vector3(transform.position.x + attackDistance * facingDir, transform.position.y));
}
}
SkeletonGroundState.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SkeletonGroundState : EnemyState
{
protected Transform player;
public Enemy_Skeleton enemy;
public SkeletonGroundState(EnemyStateMachine _stateMachine, Enemy _enemyBase, string _animBoolName, Enemy_Skeleton _enemy) : base(_stateMachine, _enemyBase, _animBoolName)
{
this.enemy = _enemy;
}
public override void Enter()
{
base.Enter();
player = GameObject.Find("Player").transform;
}
public override void Exit()
{
base.Exit();
}
public override void Update()
{
base.Update();
if(enemy.IsPlayerDetected()|| Vector2.Distance(enemy.transform.position,player.transform.position)<2)
{
stateMachine.ChangeState(enemy.battleState);
}
}
}
SkeletonBattleState.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SkeletonBattleState : EnemyState
{
private Transform player;
Enemy_Skeleton enemy;
private int moveDir;
public SkeletonBattleState(EnemyStateMachine _stateMachine, Enemy _enemyBase, string _animBoolName, Enemy_Skeleton _enemy) : base(_stateMachine, _enemyBase, _animBoolName)
{
this.enemy = _enemy;
}
public override void Enter()
{
base.Enter();
player = GameObject.Find("Player").transform;//获得信息player
}
public override void Exit()
{
base.Exit();
}
public override void Update()
{
base.Update();
if(enemy.IsPlayerDetected())
{
stateTimer = enemy.battleTime;
if(enemy.IsPlayerDetected().distance<enemy.attackDistance)
{
if(CanAttack())
stateMachine.ChangeState(enemy.attackState);
}
}
else
{
if(stateTimer < 0||Vector2.Distance(player.transform.position,enemy.transform.position)>7)
{
stateMachine.ChangeState(enemy.idleState);
}
}
if(player.position.x > enemy.transform.position.x)
{
moveDir = 1;
}
else if(player.position.x < enemy.transform.position.x)
{
moveDir = -1;
}
enemy.SetVelocity(enemy.moveSpeed * moveDir, rb.velocity.y);
}
private bool CanAttack()
{
if(Time.time >= enemy.lastTimeAttacked+enemy.attackCooldown)
{
enemy.lastTimeAttacked = Time.time;
return true;
}
Debug.Log("Attack cool");
return false;
}
}