『功能项目』宠物的召唤跟随【79】

我们打开上一篇78装备齐全特效的项目,

本章要做的事情是实现宠物跟随功能

首先创建一个宠物召唤界面

重命名按钮组件为CallPetBtn

重命名Image组件为PetExample

宠物资源所在资源文件夹

在主角预制体中的身后位置设置一个宠物跟随点

创建宠物动画控制器

拖拽至Pet01预制体动画控制器框选

创建脚本:PetFollowTarget.cs

实现宠物到达目标点就播放Idle动画,未到目标点就朝着目标点前进

cs 复制代码
using UnityEngine;
public class PetFollowTarget : MonoBehaviour{
    Transform target; 
    float speed = 2f; 
    Animator animator;
    void Start(){
        target = GameObject.Find("PlayerNormal/PetsSmallPos").gameObject.transform;
        animator = GetComponent<Animator>(); 
    }
    void Update(){
        if (target != null){
            Vector3 direction = target.position - transform.position;
            float distance = direction.magnitude;
            if (distance > 0.1f){
                animator.SetBool("IsMoving", true);
                transform.LookAt(target.position);
                transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
            }
            else{
                animator.SetBool("IsMoving", false);
            }
        }
    }
}

修改脚本:CallOrHidePet.cs

cs 复制代码
using UnityEngine;
using UnityEngine.UI;
public class CallOrHidePet : MonoBehaviour{
    Button callBtn;
    GameObject petPrefab;
    GameObject petInstance;
    void Start(){
        petPrefab = Resources.Load<GameObject>("Prefabs/Pets/Pet01Small");
        callBtn = transform.Find("Image/CallPetBtn").GetComponent<Button>(); ;
        callBtn.onClick.AddListener(OnCallButtonClick);
    }
    public void OnCallButtonClick(){

        if (petInstance == null){
            petInstance = Instantiate(petPrefab,
                GameObject.FindWithTag("Player").transform.position + new Vector3(-2f, 0, -1f),
                Quaternion.identity);
            petInstance.AddComponent<PetFollowTarget>();
        }
        else{
            if (GameObject.Find("PlayerNormal") == null)
                return;
            else
                Destroy(petInstance);
        }
    }
    void OnDestroy(){
        Button callBtn = transform.Find("Image/CallPetBtn").GetComponent<Button>();
        if (callBtn != null)
            callBtn.onClick.RemoveListener(OnCallButtonClick);
    }
}

修改脚本:UIManager.cs

实现开关宠物界面及召唤关闭宠物

运行项目

本章做了实现了开关宠物UI界面,召唤取消宠物,宠物跟随主角身后Pos点的功能

接下来的文章内容:

1.宠物随机攻击的巨型化显示

2.窗口可拖拽脚本

3.点击名称寻找地点功能

4.隐藏怪物的生成

5.怪物I攻击范围内的主动攻击

6.掉落坐骑蛋的获取

7.异步传送转换场景

以及开放回合制、坐骑系统、宠物系统、背包系统、神炼系统、商城系统、Boss的目标跟随任务导航系统以及UI播放3D动画效果等等。

具体项目运行效果请关注water1024的b站视频项目演示《破碎纪元》

water1024的个人空间-water1024个人主页-哔哩哔哩视频 (bilibili.com)https://space.bilibili.com/1050927514?spm_id_from=333.1007.0.0

相关推荐
Tummer83633 小时前
C#+WPF+prism+materialdesign创建工具主界面框架
开发语言·c#·wpf
ghost1433 小时前
C#学习第23天:面向对象设计模式
开发语言·学习·设计模式·c#
yngsqq4 小时前
(for 循环) VS (LINQ) 性能比拼 ——c#
c#·solr·linq
想做后端的小C5 小时前
C# 面向对象 构造函数带参无参细节解析
开发语言·c#·面向对象
炯哈哈5 小时前
【上位机——WPF】App.xml和Application类简介
xml·开发语言·c#·wpf·上位机
bestcxx5 小时前
c# UTC 时间赋值注意事项
c#·utc
酷炫码神5 小时前
C#运算符
开发语言·c#
zybsjn6 小时前
后端系统做国际化改造,生成多语言包
java·python·c#
敲代码的 蜡笔小新7 小时前
【行为型之迭代器模式】游戏开发实战——Unity高效集合遍历与场景管理的架构精髓
unity·设计模式·c#·迭代器模式
yc_12247 小时前
SqlHelper 实现类,支持多数据库,提供异步操作、自动重试、事务、存储过程、分页、缓存等功能。
数据库·c#