没有合适的资源?快使用搜索试试~ 我知道了~
Unity状态机设计
资源推荐
资源详情
资源评论
怪物巡逻状态机
1 人物距离大于10米 每3秒巡逻一次
2 人物距离小于10米 向人物移动
3 人物距离小于2.5米 向人物攻击
人物状态机
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerStateManager : MonoBehaviour {
private PlayerStatebase currentState;
private Dictionary<System.Type, PlayerStatebase> stateDic;
void Awake()
{
stateDic = new Dictionary<System.Type, PlayerStatebase>();
AddState<PlayerStateIdle>();
AddState<PlayerStateRun>();
AddState<PlayerStateAttack1>();
AddState<PlayerStateAttack2>();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
人物状态父类
AddState<PlayerStateAttack3>();
ChangeState<PlayerStateIdle>();
}
void AddState<T>() where T : PlayerStatebase
{
PlayerStatebase state = gameObject.AddComponent<T>();
state.OnInit();
stateDic.Add(state.GetType(), state);
}
public void ChangeState<T>() where T : PlayerStatebase
{
if (stateDic.ContainsKey(typeof(T)))
{
PlayerStatebase state = stateDic[typeof(T)];
currentState = state;
currentState.OnEnter();
}
}
void Update()
{
if (currentState != null)
currentState.OnExcute();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerStatebase : MonoBehaviour {
protected Animator animator;
protected string animName;
protected PlayerStateManager manager;
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
1
2
3
4
5
6
7
8
9
10
人物待机
public virtual void OnInit()
{
animator = GetComponent<Animator>();
manager = GetComponent<PlayerStateManager>();
}
public virtual void OnEnter() { }
public virtual void OnExcute() { }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerStateIdle : PlayerStatebase {
float h, v;
public override void OnInit()
{
base.OnInit();
animName = "attack_idle";
}
public override void OnEnter()
{
animator.SetInteger("State", 0);
}
public override void OnExcute()
{
if (!animator.GetCurrentAnimatorStateInfo(0).IsName(animName))
return;
if (Input.GetMouseButtonDown(0))
{
manager.ChangeState<PlayerStateAttack1>();
return;
11
12
13
14
15
16
17
18
19
20
21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
剩余15页未读,继续阅读
资源评论
天宇琪云
- 粉丝: 41
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功