# GameMoveDemo **Repository Path**: jasonkevin88/game-move-demo ## Basic Information - **Project Name**: GameMoveDemo - **Description**: 当前示例是用来编写常规的unity原生实现的物体运动和利用DoTween的物体运动 - **Primary Language**: C# - **License**: AGPL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-07-10 - **Last Updated**: 2024-07-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # GameMoveDemo #### 介绍 当前项目目前有四部分知识点内容 1. 利用unity自带的`Vector3.MoveTowards`方法不断地更新位置进行移动 2. 利用Unity的第三方插件[dotween](https://dotween.demigiant.com/)实现按照自定的路径进行移动 3. 实现角色/物体上有名称跟随 4. 实现角色血条 ##### 创建血条 1. 创建血条 UI 元素: 使用 Unity 的 Canvas 系统来创建血条 UI。 在 Canvas 下创建一个 Image 作为血条的背景。 在背景 Image 下再创建一个 Image 作为实际显示血量的前景条。Image需要设置图片,并且把ImageType设置成Filled。 2. 设置血条 UI 的位置和大小: 为了使 UI 元素能跟随角色,可以将 Canvas 的渲染模式设置为世界空间(World Space)。 选择 Canvas,在 Inspector 面板中将其渲染模式设置为 World Space。 调整 Canvas 的大小和位置,使其适合场景中的 UI 显示。 3. 编写脚本更新血条位置 ``` public class EnemyHealthFollow : MonoBehaviour { public Transform target; // 角色 Transform public Vector3 offset; // 血条相对于角色的偏移量 void Update() { if (target != null) { // 将血条的位置设置为目标位置加上偏移量 transform.position = target.position + offset; } } } ``` 4. 绑定 UI 元素和脚本 将 HealthBarFollow 脚本附加到血条的根对象(Canvas 或空的父对象)。 将角色的 Transform 拖动到 HealthBarFollow 脚本的 target 字段中。 调整 offset 字段,使血条在合适的位置显示。 #### 软件架构 软件架构说明 5. 编写控制脚本 创建一个名为 HealthBar 的脚本,并将其附加到角色 GameObject 上。 ``` public class HealthBar : MonoBehaviour { public Image foregroundImage; // 前景条 Image public float updateSpeedSeconds = 0.5f; // 血条更新速度 private void Awake() { GetComponentInParent().OnHealthChanged += HandleHealthChanged; } private void HandleHealthChanged(float pct) { Debug.Log("pct = " + pct); StartCoroutine(ChangeToPct(pct)); } private IEnumerator ChangeToPct(float pct) { float preChangePct = foregroundImage.fillAmount; float elapsed = 0f; Debug.Log("preChangePct = "+ preChangePct + " - pct = "+ pct); while (elapsed < updateSpeedSeconds) { elapsed += Time.deltaTime; // 使用fillAmount的,条件是Image必须要设置SourceImage, 同时ImageType设置成Filled foregroundImage.fillAmount = Mathf.Lerp(preChangePct, pct, elapsed / updateSpeedSeconds); yield return null; } foregroundImage.fillAmount = pct; } } ``` 6. 在角色脚本中管理血量 假设有一个角色脚本 Character,其负责管理角色的血量。 ``` public class Character : MonoBehaviour { // 血量改变事件 public event Action OnHealthChanged; // 最大血量 public float maxHealth = 100f; // 当前的血量 private float currentHealth; // Start is called before the first frame update void Start() { currentHealth = maxHealth; } // Update is called once per frame void Update() { } /// /// 收到攻击 /// /// 消耗的血量 public void TakeDamage(float damage) { Debug.Log("damage = "+damage); currentHealth -= damage; float currentHealthPct = currentHealth / maxHealth; OnHealthChanged?.Invoke(currentHealthPct); } } ``` 将 HealthBar 脚本附加到角色 GameObject 上。 在 HealthBar 脚本的 foregroundImage 字段中,拖动对应的前景条 Image。 确保 Character 脚本也附加到角色 GameObject 上。 #### 安装教程 当前用的是2020.3.48f1c1,建议编译运行的时候,同版本或者比这个版本高 #### 参考文献 1. [【Unity案例小技巧】Unity如何制作血条,头顶血条<血条的制作小技巧>](https://blog.csdn.net/m0_64128218/article/details/137993490) 2. [Unity 物体的朝向与旋转](https://blog.csdn.net/qq_21743659/article/details/133754364) 3. [介绍DOTween以及其常用函数属性介绍](https://blog.csdn.net/dangoxiba/article/details/124670823) 4. [DOTween的基本用法](https://www.cnblogs.com/xiaoyulong/p/10041489.html) 5. [Unity DOTween插件常用方法](https://blog.csdn.net/jc15274630894/article/details/135948862) 6. [DOTween教程☀️DOTween的使用教程](https://blog.csdn.net/weixin_38239050/article/details/126664520) 7. [Unity Canvas 组件详解:渲染模式、屏幕适配](https://zhuanlan.zhihu.com/p/643275125)