麦扣 3DRPG游戏开发教程 8-11集

麦扣 3DRPG游戏开发教程 8-11集

🎮

08 动画控制器

首先在动画系统内添加 blend tree 设置好动画

随后在代码内控制速度对应的动画播放

注:注释的代码是不相关的代码,是前面的

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
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class PlayerController : MonoBehaviour
{
//private NavMeshAgent agent;
private Animator anim;
//首先获取 animator

private void Awake()
{
//agent = GetComponent<NavMeshAgent>();
anim = GetComponent<Animator>();
}

//private void Start()
//{
//MouseManager.Instance.OnMouseClicked += MoveToTarget;
//这是一个事件
//}


private void Update()
{
SwitchAnimation();
}

void SwitchAnimation()
{
anim.SetFloat("Speed", agent.velocity.sqrMagnitude);
//将agent的速度传给动画
}
//void MoveToTarget(Vector3 target)
//{
//agent.destination = target;
//}

}

09 改进细节 (shader graph)

xGlVWn.png


遮挡

xGGWgs.png


取消树的射线 改变树的图层为 🐉忽略射线🐉

第二种方法:由于代码中的碰撞是collider,只需要取消物体的mesh collider 即可

10 设置敌人

将史莱姆和蜗牛的材质都升级到urp材质

创建个脚本叫 enemy controller.cs

1
2
[RequireComponent(typeof(NavMeshAgent))]
//将脚本拖拽到物体上 会自动创建一个NavMeshAgent

敌人的遮挡剔除

设置一个图层为enemy

回到pipline settings找到render 在layer mask中 选中enemy(那个是可以多选的),同样infront内layer mask 也要选上

p02fs.md.png

修改鼠标指针贴图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//回到MouseManager.cs
void SetCursorTexture()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//鼠标的位置与相机的催化剂
if (Physics.Raycast(ray, out hitInfo))
{
//切换鼠标贴图
switch (hitInfo.collider.gameObject.tag)
{
case "Ground":
Cursor.SetCursor(target,new Vector2(16,16),CursorMode.Auto);
//纹理,锚点,切换模式
break;
-----------------------
case "Enemy":
Cursor.SetCursor(attack,new Vector2(16,16),CursorMode.Auto);
//当鼠标指针掠过敌人,指针会变成🗡
break;
-----------------------
}
}
}


😀

xJFOfO.png


11 点击敌人玩家跑过去攻击动画

协程

//订阅事件

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class PlayerController : MonoBehaviour
{
private NavMeshAgent agent;
private Animator anim;
private GameObject attackTarget;

private float lastAttackTime;

//攻击冷却时间
private void Awake()
{
agent = GetComponent<NavMeshAgent>();
anim = GetComponent<Animator>();
}

private void Start()
{
MouseManager.Instance.OnMouseClicked += MoveToTarget;
//这是一个事件
MouseManager.Instance.OnEnemyClicked += EventAttack;
}


private void Update()
{
SwitchAnimation();
lastAttackTime -= Time.deltaTime;
}

void SwitchAnimation()
{
anim.SetFloat("Speed", agent.velocity.sqrMagnitude);
//将agent的速度传给动画
}

void MoveToTarget(Vector3 target)
{
//⭐核心⭐
agent.destination = target;
agent.isStopped = false;

}

private void EventAttack(GameObject target)
{
if (target != null)
{
attackTarget = target;
StartCoroutine(MoveToAttackTarget());
}
}

//延迟
IEnumerator MoveToAttackTarget()
{
agent.isStopped = false;
//转向这个攻击目标
transform.LookAt(attackTarget.transform);
//判断攻击距离 其实就是个检测条件
while (Vector3.Distance(attackTarget.transform.position, transform.position) > 1)
{

agent.destination = attackTarget.transform.position;
yield return null;
}
//一旦跑到接近点
agent.isStopped = true;
//attack
if (lastAttackTime<0)
{
anim.SetTrigger("Attack");
lastAttackTime = 0.5f;
}
}
}

//获取GameObject的坐标

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

// using UnityEngine.Events;

// [System.Serializable]
// public class EventVector3 : UnityEvent<Vector3>


public class MouseManager : MonoBehaviour
{
// public EventVector3 OnmouseClicked;
private RaycastHit hitInfo;
public static MouseManager Instance;
public event Action<Vector3> OnMouseClicked;
public event Action<GameObject> OnEnemyClicked;
public Texture2D point, doorway, attack, target, arrow;

private void Awake()
{
if (Instance != null)
{
Destroy(gameObject);
}

Instance = this;
}

private void Update()
{
SetCursorTexture();
MouseControl();
}

void SetCursorTexture()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//鼠标的位置与相机的催化剂
if (Physics.Raycast(ray, out hitInfo))
{
//切换鼠标贴图
switch (hitInfo.collider.gameObject.tag)
{
case "Ground":
Cursor.SetCursor(target,new Vector2(16,16),CursorMode.Auto);
//纹理,锚点,切换模式
break;
case "Enemy":
Cursor.SetCursor(attack,new Vector2(16,16),CursorMode.Auto);
//当鼠标指针掠过敌人,指针会变成🗡
break;
}
}
}

void MouseControl()
{
if (Input.GetMouseButtonDown(0) && hitInfo.collider != null)
{
if (hitInfo.collider.gameObject.CompareTag("Ground"))
{
OnMouseClicked?.Invoke((hitInfo.point));
}
------------------------
if (hitInfo.collider.gameObject.CompareTag("Enemy"))
{
OnEnemyClicked?.Invoke((hitInfo.collider.gameObject));
}
------------------------
}
}
}

//打断攻击动画

在移动方法内添加终止协程

1
2
3
4
5
6
7
8
9
void MoveToTarget(Vector3 target)
{
//打断攻击动画
StopAllCoroutines();
//⭐核心⭐
agent.destination = target;
agent.isStopped = false;

}

麦扣 3DRPG游戏开发教程 8-11集

https://www.llbwy.com/2022/10/09/b站m_studio3drpg8-11/

作者

发布于

2022-10-09

更新于

2022-12-19

许可协议