《游戏-02_3D-开发》之—基础框架与摄像机跟随

导入Xml文档,

<?xml version="1.0" encoding="UTF-8"?>

<Root>

<TalkInfo>

<Item Idx="0" Title="见到公子" Msg="我叫粘贴,我们村叫粘家村,是这个村子的护卫,最近村子附近有倭寇出没,请勇士出手相助!_驱逐倭寇,责无旁贷!他们的位置在哪?_他们在城堡南面_等我的好消息吧!" FHead="2" SHead="1" Money="0" Objs="" Mission="0" />

<Item Idx="1" Title="收取报酬" Msg="那帮倭寇已经被我干掉了!_感谢壮士,请问勇士尊姓大名?_在下爽!爽啊!_感谢爽壮士,这有黄金200两,请笑纳!_哈哈哈哈,这多不好意思啊!能再多点吗?" FHead="1" SHead="2" Money="200" Objs="" Mission="-1"/>

<Item Idx="2" Title="见到爽" Msg="爽壮士,这段时间村子里有狼人出没。有很多人被狼人杀害了,就在昨天村东头的闲汉刘兴浩,就被狼人刀了。_放心吧,我是预言家,我能查出谁是狼人!_那就谢谢爽壮士了_不用客气,把钱准备好就行!!!" FHead="2" SHead="1" Money="0" Objs="" Mission="1"/>

<Item Idx="3" Title="感谢爽" Msg="狼人已经被我杀了!_爽壮士果然厉害!_哈哈哈,没说的,钱呢?_钱本来已经准备好了,但是昨天来了一个叫冯圣的女巫,把钱抢跑了!_敢抢我爽的钱,丫是不想活了,她现在在哪里?_她抢完钱就跑了,不知道跑到哪里去了!" FHead="1" SHead="2" Money="0" Objs="" Mission="2"/>

</TalkInfo>

<MissionInfo>

<Item Idx="0" Title="消灭倭寇!" Msg="找到并消灭城堡南面的倭寇!" Exp="200" Money="330" Objs="幽华剑_幽华剑" TargetEnemy="TargetJapan" Mission="1"/>

<Item Idx="1" Title="消灭狼人!" Msg="找到狼人,并消灭它。" Exp="200" Money="430" Objs="浮光剑_金丝甲" TargetEnemy="TargetWolf" Mission="-1"/>

<Item Idx="2" Title="抢回黄金!" Msg="找到冯圣,夺回被抢走的黄金。" Exp="200" Money="500" Objs="腾云靴" TargetEnemy="" Mission="-1"/>

</MissionInfo>

<DrugInfo>

<Item Idx="0" Name="大还丹" Type="Hp" Hp="100" Mp="0" Msg="初级丹药"/>

<Item Idx="1" Name="金光丹" Type="Hp" Hp="200" Mp="0" Msg="中级丹药" />

<Item Idx="2" Name="碧霞丹" Type="Hp" Hp="500" Mp="0" Msg="0"/>

<Item Idx="3" Name="幽云丹" Type="Hp" Hp="1000" Mp="0" Msg="0"/>

<Item Idx="4" Name="还魂丹" Type="Hp" Hp="2000" Mp="0" Msg="0"/>

<Item Idx="5" Name="人参" Type="Mp" Hp="0" Mp="100" Msg="0"/>

<Item Idx="6" Name="灵芝" Type="Mp" Hp="0" Mp="200" Msg="0"/>

<Item Idx="7" Name="育神花" Type="Mp" Hp="0" Mp="500" Msg="0"/>

<Item Idx="8" Name="流光草" Type="Mp" Hp="0" Mp="1000" Msg="0"/>

<Item Idx="9" Name="圣心丹" Type="Mp" Hp="0" Mp="2000" Msg="0"/>

</DrugInfo>

<EquipInfo>

<Item Idx="10" Name="青锋剑" Type="Weapon" Att="5" Def="0" Mdf="0" Spd="0" Msg="0"/>

<Item Idx="11" Name="幽华剑" Type="Weapon" Att="10" Def="0" Mdf="0" Spd="0" Msg="0"/>

<Item Idx="12" Name="寒霜剑" Type="Weapon" Att="20" Def="0" Mdf="0" Spd="0" Msg="0"/>

<Item Idx="13" Name="浮光剑" Type="Weapon" Att="30" Def="0" Mdf="0" Spd="0" Msg="0"/>

<Item Idx="14" Name="天一剑" Type="Weapon" Att="50" Def="0" Mdf="0" Spd="0" Msg="0"/>

<Item Idx="15" Name="皮甲" Type="Armor" Att="0" Def="0" Mdf="5" Spd="0" Msg="0"/>

<Item Idx="16" Name="青铜甲" Type="Armor" Att="0" Def="0" Mdf="10" Spd="0" Msg="0"/>

<Item Idx="17" Name="细纹甲" Type="Armor" Att="0" Def="0" Mdf="20" Spd="0" Msg="0"/>

<Item Idx="18" Name="金丝甲" Type="Armor" Att="0" Def="0" Mdf="30" Spd="0" Msg="0"/>

<Item Idx="19" Name="圣光甲" Type="Armor" Att="0" Def="0" Mdf="50" Spd="0" Msg="0"/>

<Item Idx="20" Name="战盔" Type="Helmet" Att="1" Def="1" Mdf="1" Spd="0" Msg="精铁打造,威风凛凛。"/>

<Item Idx="21" Name="朝天冠" Type="Helmet" Att="3" Def="3" Mdf="3" Spd="0" Msg="此冠是著名锻造大师粘公子生前得意制作,坚固无比且冬凉夏暖无比销魂,带上它保你欲仙欲死!!!"/>

<Item Idx="22" Name="紫玉冠" Type="Helmet" Att="5" Def="5" Mdf="5" Spd="0" Msg="头盔正中镶嵌一个赤色宝玉,有提神醒脑之功效。"/>

<Item Idx="23" Name="杀神盔" Type="Helmet" Att="10" Def="10" Mdf="10" Spd="0" Msg="相传是战国杀神白起所用之盔 ,盔内蕴藏着白起的魂魄!"/>

<Item Idx="24" Name="紫电盔" Type="Helmet" Att="20" Def="20" Mdf="20" Spd="0" Msg="传说是盘古开天之时,混沌初开,空中闪电肆虐,盘古大展神通将闪电能量化作这个紫电盔!"/>

<Item Idx="25" Name="棉靴" Type="Boots" Att="0" Def="0" Mdf="0" Spd="2" Msg="采用新疆上等棉花制作,保暖透气,轻便舒适。"/>

<Item Idx="26" Name="青云履" Type="Boots" Att="0" Def="0" Mdf="0" Spd="4" Msg="附带轻身特性,穿上它使人身轻如燕。"/>

<Item Idx="27" Name="腾云靴" Type="Boots" Att="0" Def="0" Mdf="0" Spd="6" Msg="足踏此靴,有腾云驾雾之感。"/>

<Item Idx="28" Name="掠影靴" Type="Boots" Att="0" Def="0" Mdf="0" Spd="8" Msg="浮光掠影,岂是一个快字了得!出自锻造大师粘公子之手。"/>

<Item Idx="29" Name="登天履" Type="Boots" Att="0" Def="0" Mdf="0" Spd="20" Msg="一步登天,不过如此!"/>

<Item Idx="30" Name="木盾" Type="Shield" Att="0" Def="5" Mdf="0" Spd="0" Msg="木制简盾。"/>

<Item Idx="31" Name="金刚盾" Type="Shield" Att="0" Def="10" Mdf="0" Spd="0" Msg="不动金刚,立地为盾!"/>

<Item Idx="32" Name="蓝晶盾" Type="Shield" Att="0" Def="20" Mdf="0" Spd="0" Msg="采用稀有蓝晶矿石做成的盾,防御力超强!"/>

<Item Idx="33" Name="四象盾" Type="Shield" Att="0" Def="30" Mdf="0" Spd="0" Msg="传说集合上古四象之力做成的巨盾!"/>

<Item Idx="34" Name="蚩尤盾" Type="Shield" Att="0" Def="50" Mdf="0" Spd="0" Msg="传说盾牌之内融合这上古魔神蚩尤的魂魄!"/>

</EquipInfo>

</Root>

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

双击LoadManager.cs修改代码:

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,

双击GameManager.cs修改代码:

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文件夹下创建脚本MainGame,

双击MainGame.cs修改代码:

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,

双击People.cs修改代码:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class People : MonoBehaviour{

}

再创建两个子类MyPlayer,

双击MyPlayer.cs修改代码:

第二个子类,

双击Enemy.cs修改代码:

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,

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

将DataObject脚本挂载到Player人物上,

在脚本文件夹下创建SkillBase文件夹,

新建SkillBase.cs

双击SkillBase.cs修改脚本:

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(){

}

}

修改MyPlayer脚本代码:

using UnityEngine;

public class MyPlayer : People{

[Header("==============子类变量==============")]

public Transform toolPanel;

new void Start(){

base.Start();

}

private void Update(){

}

}

在Living脚本文件夹下新建脚本CameraCtrl,

双击CameraCtrl.cs修改代码:

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代码:

using UnityEngine;

using UnityEngine.UI;

public class MyPlayer : People{

[Header("==============子类变量==============")]

public Transform toolPanel;

public Transform skillPanel;

CharacterController contro;

float rvalue;

float spdFast = 1;

bool isHold;

GameObject sword;

GameObject swordBack;

public Image imageHp;

public Image imageMp;

new void Start(){

base.Start();

}

void Update(){

}

}

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

调节CameraCtrl数值,

运行即完成,

End.

相关推荐
呆呆小雅1 小时前
C#关键字volatile
java·redis·c#
boligongzhu1 小时前
DALSA工业相机SDK二次开发(图像采集及保存)C#版
开发语言·c#·dalsa
web147862107231 小时前
C# .Net Web 路由相关配置
前端·c#·.net
Jasmine_llq2 小时前
《 火星人 》
算法·青少年编程·c#
军训猫猫头4 小时前
20.抽卡只有金,带保底(WPF) C#
ui·c#·wpf
希忘auto4 小时前
详解Redis的常用命令
redis·1024程序员节
向宇it14 小时前
【从零开始入门unity游戏开发之——C#篇25】C#面向对象动态多态——virtual、override 和 base 关键字、抽象类和抽象方法
java·开发语言·unity·c#·游戏引擎
向宇it15 小时前
【从零开始入门unity游戏开发之——C#篇24】C#面向对象继承——万物之父(object)、装箱和拆箱、sealed 密封类
java·开发语言·unity·c#·游戏引擎
yaosheng_VALVE19 小时前
探究全金属硬密封蝶阀的奥秘-耀圣控制
运维·eclipse·自动化·pyqt·1024程序员节
dami_king20 小时前
SSH特性|组成|SSH是什么?
运维·ssh·1024程序员节