当前位置: 首页 > news >正文

RPG项目01_脚本代码

基于“RPG项目01_场景及人物动画管理器”,我们创建一个XML文档

在资源文件夹下创建一个文件夹,

命名为Xml

将Xnl文档拖拽至文件夹中,

再在文件夹的Manager下新建脚本LoadManager

写代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoadManager 
{
    public static AudioClip LoadAudio(string str) {
        AudioClip clip = Resources.Load<AudioClip>("Audio/" + str);
        return clip;
    }
    public static GameObject LoadGameObject(string str)
    {
        GameObject obj = Resources.Load<GameObject>("Prefab/" + str);
        return obj;
    }
    public static Sprite LoadSprite(string str)
    {
        Sprite sprite = Resources.Load<Sprite>("Pic/" + str);
        return sprite;
    }
    public static TextAsset LoadXml(string str)
    {
        TextAsset t = Resources.Load<TextAsset>("Xml/" + str);
        return t;
    }
}
继续在Manager文件夹下创建脚本

新建GameManager脚本:

using System.Collections.Generic;
using System.Xml;
using Unity.VisualScripting;
using UnityEngine;
public enum GameState { Play, Menu };
public class GameManager{
    //当只需要一个的时候使用静态类
    public static GameState gameState = GameState.Play; 
    public static void Init()
    {
        //SetGoods();
    }
    public static T FindType<T>(Transform t, string n)
    {
        return t.Find(n).GetComponent<T>();
    }
    public static T ParseEnum<T>(string value)
    {
        return (T)System.Enum.Parse(typeof(T), value, true);
    }
}
继续在Manager文件夹下创建脚本

写代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MainGame : MonoBehaviour
{
    public static Transform canvas;
    private void Awake()
    {
        GameManager.Init();
        canvas = transform;
    }
}
再在Scripts脚本文件夹下新建文件夹命名为:Living(活着的生物)

在Living创建基类People(狼人也是人)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class People : MonoBehaviour{
    
}
再创建两个子类:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MyPlayer : People
{
    
}

第二个子类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : People
{
    
}
重新修改MainGame代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MainGame : MonoBehaviour
{
    public static Transform canvas;
    public static MyPlayer player;
    private void Awake()
    {
        GameManager.Init();
        player = GameObject.Find("Player").GetComponent<MyPlayer>();    
        canvas = transform;
    }
}

接下来挂载脚本:

再挂载人物脚本:

新建脚本文件夹Data

新建脚本DataObject

写代码(数据类:为角色提供数据):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DataObject : MonoBehaviour 

    public string _name;
    public int hp = 100;
    public int mp = 100;
    public int hpMax = 100;
    public int mpMax = 100;
    public int lv;
    [Header("速度:")]
    public float spd = 3;
    [Header("攻击:")]
    public int att;
    [Header("防御:")]
    public int def;
    [Header("魔抗:")]
    public int mdf;
    [Header("经验价值:")]
    public int expValue;
    [Header("金钱价值:")]
    public int goldValue;
    [Header("攻击力稳定值:")]
    public int randomAtk;
}
将DataObject脚本挂载到Player人物上

运行

在文件夹下创建SkillBase

写代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public enum SkillType { Up, Magic, Physics };

public class SkillBase : MonoBehaviour
{
    
}
修改People代码:

using System.Collections.Generic;
using UnityEngine;
public class People : MonoBehaviour{
    //等价知识点:1 = 2
    //1.public int Num { get; }
    //------------------------------
    //2.int num;
    //  public int Num(){
    //      return num;
    //  }
    public DataObject data;
    public int Hp {
        protected set { 
            data.hp = value;
        }
        get {
            return Mathf.Clamp(data.hp, 0, HpMax);
        }
    }
    public int HpMax {
        protected set {
            data.hpMax = value;
        }
        get {
            return data.hpMax + OffsetHp;
        }
    }
    public int Mp
    {
        protected set
        {
            data.mp = value;
        }
        get
        {
            return Mathf.Clamp(data.mp, 0, MpMax);
        }
    }
    public int MpMax
    {
        protected set
        {
            data.mpMax = value;
        }
        get
        {
            return data.mpMax + OffsetMp;
        }
    }
    public float Spd {
        protected set { 
            data.spd = value;
        }
        get {
            return data.spd + OffsetSpd;
        }
    }
    public int Att
    {
        protected set
        {
            data.att = value;
        }
        get
        {
            return (int)(data.att * GetRandomRate()) + OffsetAtt;
        }
    }
    public int Def
    {
        protected set
        {
            data.def = value;
        }
        get
        {
            return data.def + OffsetDef;
        }
    }
    public int Mdf
    {
        protected set
        {
            data.mdf = value;
        }
        get
        {
            return data.mdf + OffsetMdf;
        }

    }
    public int lv
    {
        protected set => data.lv = value;
        get => data.lv;
    }
    public int Exp { set; get; }
    public bool IsDeath { set; get; }
    public People Target { get; set; }
    public Animator Anim { get; set; }

    protected int OffsetHp { set; get; }
    protected int OffsetMp { set; get; }
    protected int OffsetSpd { set; get; }
    protected int OffsetAtt { set; get; }
    protected int OffsetDef { set; get; }
    protected int OffsetMdf { set; get; }

    public Transform attPoint;
    public delegate void Fun(People p);
    protected event Fun Dead;
    protected Dictionary<int, SkillBase> skills = new Dictionary<int, SkillBase>();

    #region 初始化
    protected virtual void InitValue()
    {
        Anim = GetComponent<Animator>();
        data = GetComponent<DataObject>();
        Dead = Death;
    }
    protected virtual void InitSkill()
    {

    }
    #endregion

    #region 事件
    public void AddEventHandle(Fun funback)
    {
        Dead += funback;
    }
    public void RemoveEventHandle(Fun funback)
    {
        Dead -= funback;
    }
    protected virtual void Death(People p)
    {
        IsDeath = true;
        Anim.SetTrigger("IsDeath");
        p.Victory(this);
        Invoke("Over", 5);
    }
    protected virtual void Victory(People p)
    {

    }
    protected void Over()
    {
        print("over");
    }
    #endregion

    #region 战斗伤害
    protected float GetRandomRate()
    {
        return (Random.Range(-data.randomAtk, data.randomAtk + 1) + 100) * 0.01f;
    }

    public virtual void BePhysicsHit(int value, People p)
    {
        if (IsDeath)
        {
            return;
        }
        Hp -= value - Def;
        UpdateUI();
        if (Hp <= 0)
        {
            Dead(p);
        }
    }
    public virtual void BeMagicHit(int value, People p)
    {
        if (IsDeath)
        {
            return;
        }
        Hp -= value - Mdf;
        UpdateUI();
        if (Hp <= 0)
        {
            Dead(p);
        }
    }
    #endregion


    #region Hp/Mp
    public virtual void AddHp(int value)
    {
        Hp += value;
        UpdateUI();
    }
    public virtual void AddMp(int value)
    {
        Mp += value;
        UpdateUI();
    }

    public float GetHpRation()
    {
        return (float)Hp / HpMax;
    }
    #endregion
    protected void Start()
    {
        InitSkill();
        InitValue();
    }
    protected virtual void UpdateUI()
    {

    }
}
知识点:Image的冷却填充作用

创建两个Image父子物体:

将父物体放置一个图片,

对子物体添加一个半透明画面

调节子物体颜色及半透明度

类型选择为填充Fill

调节即可制作冷却

知识点结束,可以把Image删了

修改MyPlayer脚本代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MyPlayer : People
{
    [Header("==============子类变量==============")]
    public Transform toolPanel;
    new void Start() {
        base.Start();
    }
    private void Update()
    {
        
    }
}
在Living脚本文件夹下新建脚本CameraCtrl

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraCtrl : MonoBehaviour
{
    public float dis;
    public float height;
    public float speed;
    Transform target;
    Vector3 targetPos;
    // Start is called before the first frame update
    void Start()
    {
        target = MainGame.player.transform;
    }

    // Update is called once per frame
    void Update()
    {
        transform.LookAt(target.position + Vector3.up * 1.5f);
        targetPos = target.forward * (-dis) + target.up * height + target.position;
    }
    private void LateUpdate()
    {
        transform.position = Vector3.Lerp(transform.position, targetPos, speed);
    }
}
更新MyPlayer代码:

         

再将CameraCtrl摄像机跟随代码挂载在Camera摄像机上

调节CameraCtrl数值后运行

如果不想运行时摄像机千里跟随,就可以将角色的位置复制给摄像机:

最后设置一下设摄像机的y轴调高一点

完成阶段代码


 

相关文章:

RPG项目01_脚本代码

基于“RPG项目01_场景及人物动画管理器”&#xff0c;我们创建一个XML文档 在资源文件夹下创建一个文件夹&#xff0c; 命名为Xml 将Xnl文档拖拽至文件夹中&#xff0c; 再在文件夹的Manager下新建脚本LoadManager 写代码&#xff1a; using System.Collections; using System…...

目标检测YOLO实战应用案例100讲-交通目标数据集构建及高性能检测算法研究与应用

目录 前言 国内外研究现状 目标检测研究现状 目标检测数据集研究现状...

浅谈Vue.js的计算属性computed

什么是computed属性 computed 属性用于声明计算属性&#xff0c;这些属性的值是基于其他响应式属性计算而来的&#xff0c;当依赖的响应式属性发生变化时&#xff0c;计算属性会自动重新计算。 与Vue.js 2相比&#xff0c;Vue.js 3的 computed 属性语法稍有变化&#xff0c;不…...

Linux常用指令详解

目录 前言&#xff1a; Linux的目录结构 Linux常用指令简介 whoami指令 ls指令 pwd指令 cd指令 tree指令 touch指令 mkdir指令 rmdir指令与rm指令 man指令 cp&#xff08;copy&#xff09;指令 mv&#xff08;move&#xff09;指令 cat指令 重定向及重定向的类型…...

Nginx(性能优化)

到这里文章的篇幅较长了&#xff0c;最后再来聊一下关于Nginx的性能优化&#xff0c;主要就简单说说收益最高的几个优化项&#xff0c;在这块就不再展开叙述了&#xff0c;毕竟影响性能都有多方面原因导致的&#xff0c;比如网络、服务器硬件、操作系统、后端服务、程序自身、数…...

机器学习笔记 - 如何在Python中对网格和点云进行体素化?

一、简述 本文主要是为了了解如何生成体素表示,体素之于3D就像像素之于2D。体素本质上是 3D 像素,但它们不是正方形,而是完美的立方体。 理论上,体素是复制现实的完美建模技术。 这里我们要了解四个广泛流行的 Python 库(Open3D、Trimesh、PyVista、pyntcloud )生成点云…...

冒个泡!OceanBase亮相 2023 新加坡金融科技节

近日&#xff0c;OceanBase 亮相 Singapore Fintech Festival 2023&#xff08;2023 新加坡金融科技节&#xff09;&#xff01;本届新加坡金融科技节于 2023 年 11 月 15 日至 17 日在新加坡博览展览中心举行&#xff0c;展会期间&#xff0c;OceanBase 得到了众多金融科技机构…...

正则表达式(5):常用符号

正则表达式&#xff08;5&#xff09;&#xff1a;常用符号 小结 本博文转载自 在本博客中&#xff0c;”正则表达式”为一系列文章&#xff0c;如果你想要从头学习怎样在Linux中使用正则&#xff0c;可以参考此系列文章&#xff0c;直达链接如下&#xff1a; 在Linux中使用正…...

Web安全漏洞分析-XSS(下)

随着互联网的迅猛发展&#xff0c;Web应用的普及程度也愈发广泛。然而&#xff0c;随之而来的是各种安全威胁的不断涌现&#xff0c;其中最为常见而危险的之一就是跨站脚本攻击&#xff08;Cross-Site Scripting&#xff0c;简称XSS&#xff09;。XSS攻击一直以来都是Web安全领…...

金南瓜SECS/GEM C# SDK 快速使用指南

本文对如何使用金南瓜SECS/GEM C# SDK 快速创建一个满足SECS/GEM通信要求的应用程序&#xff0c;只需简单3步完成。 第一步&#xff1a;创建C# .NET程序 示例使用Visual Studio 2010&#xff0c;使用者可以选择更高级版本 Visual Studio 第二步&#xff1a;添加DLL库引用&am…...

在一个没有超级用户的mongodb 生产库上如何添加超级用户

说来这个问题&#xff0c;都觉得不可思议&#xff0c;一个数据库怎么没有超级用户呢&#xff0c;我们知道&#xff0c;MYSQL&#xff0c;PG&#xff0c;ORACLE等&#xff0c;创建好后&#xff0c;都有一个默认的超级用户&#xff0c;MONGODB也有超级用户&#xff0c;但需要自己…...

排序算法之二:冒泡排序

冒泡排序的思路 冒泡排序是交换排序 基本思想&#xff1a;所谓交换&#xff0c;就是根据序列中两个记录键值的比较结果来对换这两个记录在序列中的位置&#xff0c;交换排序的特点是&#xff1a;将键值较大的记录向序列的尾部移动&#xff0c;键值较小的记录向序列的前部移动…...

一键搭建你的hnust请假条

hnust请假条 湖南科技大学请假条生成器 https://hnust.rick.icu/new &#xff08;直接使用&#xff09; Hnust Leave Note 去github https://github.com/rickhqh/hnust_leave_note 效果展示 界面展示效果图 v2.0 更新 vant和vue重构了整个源码同步学校新版请假条样式修复了…...

C练习题13

单项选择题(本大题共20小题,每小题2分,共40分。在每小题给出的四个备选项中,选出一个正确的答案,并将所选项前的字母填写在答题纸的相应位置上。) 1.结构化程序由三种基本结构组成、三种基本结构组成的算法是() A.可以完成任何复杂的任务 B. 只能完成部分复杂的任务 C. 只能完…...

交易历史记录20231206 记录

昨日回顾&#xff1a; select top 10000 * from dbo.CODEINFO A left join dbo.全部&#xff21;股20231206010101 B ON A.CODE B.代码 left join dbo.全部&#xff21;股20231206CONF D on A.CODED.代码left join dbo.全部&#xff21;股20231206 G on A.CODEG.代码 left…...

1-5总体分布的推断

...

深信服技术认证“SCSA-S”划重点:XSS漏洞

为帮助大家更加系统化地学习网络安全知识&#xff0c;以及更高效地通过深信服安全服务认证工程师考核&#xff0c;深信服特别推出“SCSA-S认证备考秘笈”共十期内容&#xff0c;“考试重点”内容框架&#xff0c;帮助大家快速get重点知识~ 划重点来啦 *点击图片放大展示 深信服…...

MIT6S081-Lab2总结

大家好&#xff0c;我叫徐锦桐&#xff0c;个人博客地址为www.xujintong.com&#xff0c;github地址为https://github.com/xjintong。平时记录一下学习计算机过程中获取的知识&#xff0c;还有日常折腾的经验&#xff0c;欢迎大家访问。 Lab2就是了解一下xv6的系统调用流程&…...

CMMI5大成熟度等级和4大过程域

CMMI&#xff08;Capability Maturity Model Integration&#xff0c;能力成熟度模型集成&#xff09;模型系列是帮助组织改进其过程的最佳实践的集合。这些模型由来自产业界、政府以及软件工程研究所&#xff08;Software Engineering Institute&#xff0c; SEI&#xff09;的…...

c++新经典模板与泛型编程:const修饰符的移除与增加

const修饰符的移除 让你来写移除const修饰符&#xff0c;你会怎么样来写&#xff1f; &#x1f602;&#x1f602;trait类模板&#xff0c;如下 #include <iostream>// 泛化版本 template<typename T> struct RemoveConst {using type T; };// 特化版本 template…...

如何使用NoFences实现高效的Windows桌面图标管理

如何使用NoFences实现高效的Windows桌面图标管理 【免费下载链接】NoFences &#x1f6a7; Open Source Stardock Fences alternative 项目地址: https://gitcode.com/gh_mirrors/no/NoFences NoFences是一款开源免费的Windows桌面管理工具&#xff0c;专门用于解决桌面…...

FPGA设计优化:如何用Vivado的opt_directive提升性能(附真实案例)

FPGA设计优化&#xff1a;Vivado的opt_directive实战指南与性能提升策略 在FPGA设计流程中&#xff0c;逻辑优化是提升性能的关键环节。Xilinx Vivado Design Suite提供的opt_design命令及其directive参数&#xff0c;为工程师提供了精细控制优化策略的能力。本文将深入探讨如何…...

Phi-3-mini-128k-instruct Chainlit集成:支持Markdown渲染、LaTeX公式与代码高亮

Phi-3-mini-128k-instruct Chainlit集成&#xff1a;支持Markdown渲染、LaTeX公式与代码高亮 1. 模型简介 Phi-3-Mini-128K-Instruct是一个38亿参数的轻量级开放模型&#xff0c;属于Phi-3系列中的高性能版本。这个模型经过精心训练&#xff0c;特别适合需要长文本理解和复杂…...

LumiPixel Canvas Quest批量处理教程:使用Python脚本自动化生成人像图库

LumiPixel Canvas Quest批量处理教程&#xff1a;使用Python脚本自动化生成人像图库 1. 引言 最近遇到一个实际需求&#xff1a;需要为电商项目快速生成5000张不同风格的人像图片。手动一张张生成显然不现实&#xff0c;于是研究出了这套基于Python的自动化方案。用下来效果不…...

DAMO-YOLO实战:用AI视觉系统做内容安全审核与统计

DAMO-YOLO实战&#xff1a;用AI视觉系统做内容安全审核与统计 1. 引言&#xff1a;当AI视觉遇见内容安全 在数字内容爆炸式增长的今天&#xff0c;如何高效地进行内容审核成为许多平台面临的挑战。传统人工审核不仅效率低下&#xff0c;而且容易因疲劳导致误判。本文将介绍如…...

Z-Image-GGUF模型解析:C语言视角下的文件读写与GGUF格式处理

Z-Image-GGUF模型解析&#xff1a;C语言视角下的文件读写与GGUF格式处理 你是不是也好奇&#xff0c;那些动辄几十GB的大模型文件&#xff0c;计算机到底是怎么“看懂”并加载它们的&#xff1f;今天我们不聊高层的API调用&#xff0c;而是拿起C语言这把“手术刀”&#xff0c…...

Windows 11安卓子系统实战:无需商店直装APK的终极指南

1. Windows 11安卓子系统核心概念解析 Windows 11安卓子系统&#xff08;Windows Subsystem for Android&#xff0c;简称WSA&#xff09;是微软推出的重磅功能&#xff0c;它让Windows系统首次实现了原生运行安卓应用的能力。这个功能本质上是在Windows内核层构建了一个轻量化…...

Cursor Pro功能解锁指南:突破限制的完整技术方案

Cursor Pro功能解锁指南&#xff1a;突破限制的完整技术方案 【免费下载链接】cursor-free-vip [Support 0.45]&#xff08;Multi Language 多语言&#xff09;自动注册 Cursor Ai &#xff0c;自动重置机器ID &#xff0c; 免费升级使用Pro 功能: Youve reached your trial re…...

保姆级教程:用Docker Compose一键部署带汉化和HTTPS的n8n,并配置反向代理(Nginx)

企业级n8n自动化平台全栈部署实战&#xff1a;从容器编排到安全加固 在数字化转型浪潮中&#xff0c;自动化工作流平台已成为企业降本增效的核心基础设施。n8n作为GitHub上增长最快的开源自动化工具之一&#xff0c;凭借其可视化编排能力和400节点生态&#xff0c;正在重塑企业…...

避坑指南:Ollama部署DeepSeek-R1时,如何安全地开放API端口给内网其他服务调用?

深度解析&#xff1a;Ollama部署DeepSeek-R1时内网API安全开放实战 当你在一台Linux服务器上成功部署了Ollama和DeepSeek-R1模型后&#xff0c;下一步自然是想让内网中的其他服务也能调用这个强大的AI能力。但直接开放端口就像把家门钥匙插在锁上——方便但危险。本文将带你深入…...