英雄无敌(VR Project)【前方高能】:敌人模块、武器模块、HTC VIVE、玩家模块

可以学习考参一下本文章的思想、思路甚至是细节呦!*需求分析是重点

目录

敌人模块:

·敌人沿指定路线运动

·受击后减血死亡

·运动播放跑步动画,攻击播放攻击动画,攻击间隔播放闲置动画,死亡播放死亡动画

·到达终点,攻击玩家

敌人生成器模块

策划

需求分析:

代码实现:


敌人模块:

·敌人沿指定路线运动

需求分析:创建脚本—敌人马达EnemyMotor,提供移动、旋转,寻路功能

代码实现:

public class EnemyMotor:MonoBehaviour

{

public void MovementForward()//向前移动

public void LookRotation(Vector3 point)//point:注视的目标点

{//提示:当前物体注视目标点旋转}

public bool Pathfinding()

{ return true;//需要继续寻路

  Return false;//到达终点,无需寻路

//如果到达目标点(判断当前位置与目标点间距 Vector3.Distance)

//更新目标点(向下一路口移动)

//朝向目标点

//向前移动

}

}

using System.Collections;using System.Collections.Generic;using UnityEngine;/// <summary>/// 敌人马达,提供移动、旋转、寻路功能/// </summary>public class EnemyMotor : MonoBehaviour{public Transform[] points;//当有路线脚本时:public WayLine line;//当前路点索引private int currentPointIndex;private float moveSpeed = 2;public void MovementForward(){this.transform.Translate(0, 0, moveSpeed * Time.deltaTime);}public void LookRotation(Vector3 targetPoint){//提示:当前物体注视目标点旋转this.transform.LookAt(targetPoint);}public bool Pathfinding(){if (points == null || currentPointIndex >= points.Length) return false;//没有下一个点了//if (line== null || currentPointIndex >= line.WayPoints.Length) return false;(有了路线脚本后,改变的位置//把坐标换成路线)//朝向目标点LookRotation(points[currentPointIndex].position);//LookRotation(line.WayPoints[currentPointIndex]);//向前移动MovementForward();//如果到达目标(当前位置接近于目标点)if (Vector3.Distance(this.transform.position, points[currentPointIndex].position) <0.5f)//if(Vector3.Distance(this.transform.position,line.WayPoints[currentPointIndex]< 0.5f)currentPointIndex++;return true;//可以继续寻路}//private void Update()//{//    Pathfinding();//}}

·受击后减血死亡

需求分析:创建脚本—敌人状态信息EnemystatusInfo,定义血量,提供受伤,死亡的功能

代码实现:

public class EnemystatusInfo:MonoBehaviour

{

public EnemySpawn spawn;//敌人生成器的引用

public float HP=0;//当前血量

public float maxHP=200;//血量最大值

public float deathDelay=3;//死亡延迟销毁时间

//受伤

public void Damage(float amount)//amount需要扣除的血量

{ //扣血

HP-=amount;

//血量为0时,调用死亡方法

if(HP<=0)

Death();

}

public void Death()

{ Debug.Log(“阵亡”);

//播放当前画面

var anim=GetComponent<EnemyAnimation>();

animaction.Play(anim.deathAnimName);

//销毁当前物体

Destroy(this.gameObject,deathDelay);

//设置路线状态

GetComponent<EnemyMotor>().line.IsUsable=true;

//产生一下个敌人

Spawn.GenerateEnemy();

}

}

·运动播放跑步动画,攻击播放攻击动画,攻击间隔播放闲置动画,死亡播放死亡动画

需求分析:创建脚本—敌人动画EnemyAnimation,定义各种动画名称,提供动画播放的功能

public class EnemyAnimation:MonoBehaviour

{ //敌人动画,定义需要播放的动画片段名称

public string anim.idleAnimName;//攻击后的闲置动画

public string runAnimName;

public string attckAnimName;

public string deathAnimName

public AnimationAction action;//AnimationAction行为类

public void Awake()

{ action=new Animation GetComponentInChildren<Animation>();

}

}

//动画行为类,提供有关动画的行为

public class AnimationAction

{ //附加在敌人模型上的动画组件引用

private Animation anim;

public AnimationAction(Animation anim)

{this.anim=anim;}//得到物体的引用

//播放动画

public void Play(string animName)

{anim.CrossFade(animName);}

//判断指定动画是否正在播放

public bool IsPlaying(string animName)//animName动画片段名称

{return anim.IsPlaying(animName);}

}

·到达终点,攻击玩家

需求分析:创建脚本—敌人AI EnemyAI,通过判断状态,执行寻路或者攻击

代码实现:

敌人AI

[RequireComponent(typeof(EnemyAniamtion))]

[RequireComponent(typeof(EnemyMotor))]

[RequireComponent(typeof(EnemyStatusInfo))]

//把EnemyAI拖给脚本,上边三个脚本也直接带着

public class EnemyAI:MonoBehaviour

{

//定义敌人状态的枚举类型

public enmu State

{ Attack, //攻击状态

PathFinding //寻路状态

}private State currentState=State.PathFinding;

private EnrmyMotor motor;

private EnemyAnimation anim;

private float atkTimer;

public float atkInterval=3;//攻击间隔

public void Start()

{ anim=GetComponent<EnemyAnimation>();

motor=GetComponent<EnemyMotor>();//找马达

}

public void Update()

{ //判断

Switch(currentState)

case State.PathFinding;

//执行寻路,调用马达中寻路方法,播放跑步动画

anim.action.Play(anim.runAnimName);

if(motor.Pathfinding()=false)//执行寻路结束,把切换状态

currentState=State.Attack;

break;

case State.Attack;

Attack();

break;

}

private void Attack()

{  //如果攻击动画没有播放

if(anim.action.IsPlaying(anim.attackAnimName)==false)

anim.action.Play(anim.idleAnimName)//播放闲置动画

//发起攻击

if(atkTimer<=Time.time)

{ anim.action.Play(anim.attackAnimName);

atkTimer=Time.time+atkInterval;//下一帧再调用就不满足条件

}

}(//ctrl+R+m可以提个方法)

}

AI怎样调用的动画:先找到引用EnemyAnimation,调用action,通过action调用AnimationAction里的实例方法

敌人生成器模块

策划

·开始时生成指定数量的敌人

·为每人随机选择一条可以使用的路线

要求:敌人类型,产生的延迟时间随机

·当敌人死亡后,再产生下一个敌人,直到生成数量达到上限为止

需求分析:

·创建跟路线,并添加多条配有路点的路线

·创建脚本—敌人生成器EnemySpawn,附加到跟路线中,提供生成敌人的功能

·创建类—路线WayLine,包含属性:路点坐标Vector3[] Points,是否可用bool IsUseable

当生成器启用后,计算所有子物体[路线]的路点坐标

当生成敌人时,随机选择一条可以使用的路线

代码实现:

//路线类

public class WayLine

{ public Vector3[] WayPoints{get;set;}

public bool IsUseable{get;set;}

//成员变量声明完,WayPoints默认是null,IsUseable默认是false

}

using System.Collections;using System.Collections.Generic;using UnityEngine;public class WayLine{public Vector3[] WayPoints { get; set; }//声明完之后装的是null,因此要newpublic bool IsUseable { get; set; }public WayLine(int wayPointCount){WayPoints = new Vector3[wayPointCount];IsUseable = true;}}

//敌人生成器(脚本给路线点)

//脚本在路点,怎么找敌人?

public GameObject[] enemyType;//创建的敌人预制件数组

public int maxCount=5;//创建敌人最大数目

public int StartCount=2;

private int enemyCount;//已经创建的敌人数量

//生成一个敌人

public void GenerateEnemy()

{ //选择一条可以使用的路线?

//延迟时间随机

//Object.Instantiate(敌人预制件,位置,旋转角度)=Random.Range(0,enemyType.Length);

//创建敌人

GameObject go=Instantiate(enemyType[randomIndex],路线的第一个路点,Quaternion.identity)as GameObject;

//配置信息

go.GetComponent<EnemyMotor>();

}

using System.Collections;using System.Collections.Generic;using UnityEngine;public class EnemySpawn : MonoBehaviour{public GameObject[] enemyType;//创建敌人的预制件public int maxCount = 5;public int StartCount = 2;//开始同时创建敌人的数量private int spawnedCount;//已经创建敌人数量private int maxDelay = 10;//延迟调用的最大时间private void Start(){CalculateWayLines();//计算所有路点//创建敌人for (int i = 0; i < StartCount; i++)GenerateEnemy();}private WayLine[] lines;//路线的数组是空的private void CalculateWayLines(){lines = new WayLine[this.transform.childCount];//根据路线数量创建路线数组,路线的数组有了但具体是什么还是空的for(int i=0;i<lines.Length;i++){//每一个路线//路线变换组件的引用Transform wayLineTF = this.transform.GetChild(i);//获取每条路线的路点数//创建路线对象lines[i] = new WayLine(wayLineTF.childCount);//lines[0] {F12WayLine(WayPoints:Vector3[4]}//找点的坐标:把点的坐标给到路线的路点里去for(int pointIndex=0; pointIndex<wayLineTF.childCount; pointIndex++){   //给路线路点赋值lines[i].WayPoints[pointIndex] = wayLineTF.GetChild(pointIndex).position;}}}//选择所有可以使用的路线private WayLine[] SelectUsableWayLine(){List<WayLine> result = new List<WayLine>(lines.Length);//遍历所有路线foreach(var item in lines){//如果可以使用,添加到result列表中if (item.IsUseable) result.Add(item);}return result.ToArray();//把集合变为数组返回}public void GenerateEnemy(){spawnedCount++;//如果生成数量,已达到上限if(spawnedCount >=maxCount)return;//延迟产生一个敌人Invoke("CreateEnemy", Random.Range(1, maxDelay));}private void CreateEnemy(){//选择一条可以使用的路线?//选择所有可以使用的路线WayLine[] usableWayLines = SelectUsableWayLine();//随机选择一条WayLine line = usableWayLines[Random.Range(0, usableWayLines.Length)];//延迟时间随机?int randomIndex = Random.Range(0, enemyType.Length);//创建敌人GameObject go = Instantiate(enemyType[randomIndex], line.WayPoints[0], Quaternion.identity) as GameObject;//配置信息EnemyMotor motor = go.GetComponent<EnemyMotor>();motor.line = line;//传递路线line.IsUseable = false;//传递生成器对象引用【建议使用委托代替】(“回掉”为了敌人能调生成方法,把自己的引用传递进去)go.GetComponent<EnemyStatusInfo>().spawn = this;}}

Unity—英雄无敌(前方高能)相关推荐

  1. 英雄无敌3版的仙剑奇侠传

    英雄无敌3版的仙剑奇侠传(战役,共8张地图?).     使用方法:复制到英雄无敌3安装目录下的Maps文件夹中,进入 游戏--新游戏--战役--自创战役.     要看仙剑头像的,请把那些*.pcx ...

  2. 三国志、英雄无敌玩腻了?没关系,我教你开发个战旗游戏玩玩

    喜欢回合制战棋游戏的玩家,肯定对<三国志曹操传>和<英雄无敌>这两款经典战旗游戏不陌生吧. 在<三国志曹操传>中,镇压黄巾军.群雄讨伐董卓.灭吕布等历史事件与游戏中 ...

  3. ubuntu linux下面运行《暗黑破坏神2》和英雄无敌3-死亡阴影

    游戏文件: 链接: https://pan.baidu.com/s/1McfinqGnCZJlaCnll4o2Hw&shfl=shareset 提取码: a55t wine设置参考[2] 自己 ...

  4. Ubuntu Linux 18.10下面安装魔法门之英雄无敌3

    不废话,直接进入正题: 1.Heroes.of.Might.and.Magic.3.Linux.[mulek.info].iso 这个资源是32位 下载链接: 链接: https://pan.baid ...

  5. 英雄无敌6服务器在哪个文件夹,Win7系统无法运行英雄无敌6的两种原因和解决方法...

    英雄无敌6作为一款策略模拟类游戏,深受高端玩家的喜爱.但最近有Win7旗舰版系统用户在玩英雄无敌6时,却出现了无法运行的情况,重启好多次还是一样,不太清楚是哪里出问题,网上相关解决方案也比较少,针对此 ...

  6. 英雄无敌3 Heroes III 里面的英语单词 (转)

    英雄无敌3 Heroes III 里面的英语单词 (转)[@more@] 来自 " ITPUB博客 " ,链接:http://blog.itpub.net/10752043/vie ...

  7. 为英雄无敌3写个游戏修改器

    我是比较铁杆的英雄无敌3的fans,在网上看到这样的文章:http://game.china.com/zh_cn/play/10002765/20021113/11362720.html 就是让我方英 ...

  8. android 英雄无敌3,安卓TOP10:《英雄无敌3》高清重制版上架

    魔法门之英雄无敌3:高清版 http://img3.cache.netease.com/photo/0031/2015-01-29/600x450_AH5CQJ8T4UUJ0031.jpg http: ...

  9. 英雄无敌3 Mac 百度云 下载

    这两天看见定一直在津津有味的玩他的宙斯我表示非常羡慕.有的老游戏既是经典又是我们儿时难以忘记的回忆. 所以我决定也重温一下我的儿时难忘的回忆-英雄无敌3.只可惜我只有一台mac电脑, 本来装了虚拟机还 ...

  10. 英雄无敌3 死亡阴影 修改器

    这个找地址比帝国时代简单 本来还要做个瞬移功能 MFC还不太熟练 算了 // An highlighted block #include <iostream> #include <s ...

最新文章

  1. 论流量平台(交易内容)生死劫——币看流量生意正在进入正循环
  2. 洛谷P2518 [HAOI2010]计数
  3. ACM入门之【最小生成树】
  4. 算法---会议最大安排问题
  5. 不能在计算机网络上共享的打印机驱动程序,打印机已经共享,可是当别的电脑安装共享的打印机驱动程序时提示 windows 没法连接到打印机。拒绝访问??...
  6. 什么是 “动态规划” , 用两个经典问题举例。
  7. 音视频开发(Anychat如何改善音视频通话过程中的用户体验)
  8. cesium 百度地图_Cesium专栏-热力图(附源码下载)
  9. 关于Apache的25个初中级面试题
  10. android 导出数据库文件
  11. 不要因为不知,所以设计
  12. 2021年汽车驾驶员(初级)考试APP及汽车驾驶员(初级)考试软件
  13. it企业实习_IT企业实习心得体会范文
  14. VBA实时提取股票资金流入TOP
  15. 阿里云新增三大高性能计算解决方案,助力生命科学行业快速发展
  16. mysql cast 整数_Mysql-CAST/CONVERT 类型转换函数之 整型
  17. 国外计算机科学英语演讲,华工学子英语演讲共庆祖国华诞
  18. 39岁单身程序员入住养老院
  19. QIIME2进阶一_用QIIME2解析序列,诠释生命
  20. 关于书写--刘未鹏博客

热门文章

  1. 红帽linux如何装软件,redhat 下软件的安装
  2. Spring中实现HTTP缓存
  3. 2021基于vscode以及jlink调试esp32最新
  4. win10计算机升级系统,win10系统升级更新方法
  5. 高级信息系统项目管理师(高项)高分通过经验分享
  6. 深度残差网络+自适应参数化ReLU激活函数:调参记录17
  7. 操作系统——实时操作系统和分时操作系统
  8. 李一男荡人心魄的戏剧化人生
  9. Solidity智能合约案例——投票存在的问题
  10. python求小于n的最大素数_小于或等于n的素数