Unity—英雄无敌(前方高能)
英雄無敵(VR Project)【前方高能】:敵人模塊、武器模塊、HTC VIVE、玩家模塊
可以學(xué)習(xí)考參一下本文章的思想、思路甚至是細(xì)節(jié)呦!*需求分析是重點(diǎn)
目錄
敵人模塊:
·敵人沿指定路線運(yùn)動
·受擊后減血死亡
·運(yùn)動播放跑步動畫,攻擊播放攻擊動畫,攻擊間隔播放閑置動畫,死亡播放死亡動畫
·到達(dá)終點(diǎn),攻擊玩家
敵人生成器模塊
策劃
需求分析:
代碼實(shí)現(xiàn):
敵人模塊:
·敵人沿指定路線運(yùn)動
需求分析:創(chuàng)建腳本—敵人馬達(dá)EnemyMotor,提供移動、旋轉(zhuǎn),尋路功能
代碼實(shí)現(xiàn):
public class EnemyMotor:MonoBehaviour
{
public void MovementForward()//向前移動
public void LookRotation(Vector3 point)//point:注視的目標(biāo)點(diǎn)
{//提示:當(dāng)前物體注視目標(biāo)點(diǎn)旋轉(zhuǎn)}
public bool Pathfinding()
{ return true;//需要繼續(xù)尋路
? Return false;//到達(dá)終點(diǎn),無需尋路
//如果到達(dá)目標(biāo)點(diǎn)(判斷當(dāng)前位置與目標(biāo)點(diǎn)間距 Vector3.Distance)
//更新目標(biāo)點(diǎn)(向下一路口移動)
//朝向目標(biāo)點(diǎn)
//向前移動
}
}
using?System.Collections;using?System.Collections.Generic;using?UnityEngine;///?<summary>///?敵人馬達(dá),提供移動、旋轉(zhuǎn)、尋路功能///?</summary>public?class?EnemyMotor?: MonoBehaviour{public?Transform[] points;//當(dāng)有路線腳本時(shí):public WayLine line;//當(dāng)前路點(diǎn)索引private?int?currentPointIndex;private?float?moveSpeed = 2;public?void?MovementForward(){this.transform.Translate(0, 0, moveSpeed * Time.deltaTime);}public?void?LookRotation(Vector3 targetPoint){//提示:當(dāng)前物體注視目標(biāo)點(diǎn)旋轉(zhuǎn)this.transform.LookAt(targetPoint);}public?bool?Pathfinding(){if?(points == null?|| currentPointIndex >= points.Length) return?false;//沒有下一個(gè)點(diǎn)了//if?(line== null?|| currentPointIndex >= line.WayPoints.Length) return?false;(有了路線腳本后,改變的位置//把坐標(biāo)換成路線)//朝向目標(biāo)點(diǎn)LookRotation(points[currentPointIndex].position);//LookRotation(line.WayPoints[currentPointIndex]);//向前移動MovementForward();//如果到達(dá)目標(biāo)(當(dāng)前位置接近于目標(biāo)點(diǎn))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;//可以繼續(xù)尋路}//private void Update()//{// ???Pathfinding();//}}?
·受擊后減血死亡
需求分析:創(chuàng)建腳本—敵人狀態(tài)信息EnemystatusInfo,定義血量,提供受傷,死亡的功能
代碼實(shí)現(xiàn):
public class EnemystatusInfo:MonoBehaviour
{
public?EnemySpawn spawn;//敵人生成器的引用
public float HP=0;//當(dāng)前血量
public float maxHP=200;//血量最大值
public float deathDelay=3;//死亡延遲銷毀時(shí)間
//受傷
public void Damage(float amount)//amount需要扣除的血量
{ //扣血
HP-=amount;
//血量為0時(shí),調(diào)用死亡方法
if(HP<=0)
Death();
}
public void Death()
{ Debug.Log(“陣亡”);
//播放當(dāng)前畫面
var anim=GetComponent<EnemyAnimation>();
animaction.Play(anim.deathAnimName);
//銷毀當(dāng)前物體
Destroy(this.gameObject,deathDelay);
//設(shè)置路線狀態(tài)
GetComponent<EnemyMotor>().line.IsUsable=true;
//產(chǎn)生一下個(gè)敵人
Spawn.GenerateEnemy();
}
}
·運(yùn)動播放跑步動畫,攻擊播放攻擊動畫,攻擊間隔播放閑置動畫,死亡播放死亡動畫
需求分析:創(chuàng)建腳本—敵人動畫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>();
}
}
//動畫行為類,提供有關(guān)動畫的行為
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);}
}
·到達(dá)終點(diǎn),攻擊玩家
需求分析:創(chuàng)建腳本—敵人AI EnemyAI,通過判斷狀態(tài),執(zhí)行尋路或者攻擊
代碼實(shí)現(xiàn):
敵人AI
[RequireComponent(typeof(EnemyAniamtion))]
[RequireComponent(typeof(EnemyMotor))]
[RequireComponent(typeof(EnemyStatusInfo))]
//把EnemyAI拖給腳本,上邊三個(gè)腳本也直接帶著
public class EnemyAI:MonoBehaviour
{
//定義敵人狀態(tài)的枚舉類型
public enmu State
{ Attack, //攻擊狀態(tài)
PathFinding //尋路狀態(tài)
}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>();//找馬達(dá)
}
public void Update()
{ //判斷
Switch(currentState)
case State.PathFinding;
//執(zhí)行尋路,調(diào)用馬達(dá)中尋路方法,播放跑步動畫
anim.action.Play(anim.runAnimName);
if(motor.Pathfinding()=false)//執(zhí)行尋路結(jié)束,把切換狀態(tài)
currentState=State.Attack;
break;
case State.Attack;
Attack();
break;
}
private void Attack()
{ ?//如果攻擊動畫沒有播放
if(anim.action.IsPlaying(anim.attackAnimName)==false)
anim.action.Play(anim.idleAnimName)//播放閑置動畫
//發(fā)起攻擊
if(atkTimer<=Time.time)
{ anim.action.Play(anim.attackAnimName);
atkTimer=Time.time+atkInterval;//下一幀再調(diào)用就不滿足條件
}
}(//ctrl+R+m可以提個(gè)方法)
}
AI怎樣調(diào)用的動畫:先找到引用EnemyAnimation,調(diào)用action,通過action調(diào)用AnimationAction里的實(shí)例方法
敵人生成器模塊
策劃
·開始時(shí)生成指定數(shù)量的敵人
·為每人隨機(jī)選擇一條可以使用的路線
要求:敵人類型,產(chǎn)生的延遲時(shí)間隨機(jī)
·當(dāng)敵人死亡后,再產(chǎn)生下一個(gè)敵人,直到生成數(shù)量達(dá)到上限為止
需求分析:
·創(chuàng)建跟路線,并添加多條配有路點(diǎn)的路線
·創(chuàng)建腳本—敵人生成器EnemySpawn,附加到跟路線中,提供生成敵人的功能
·創(chuàng)建類—路線WayLine,包含屬性:路點(diǎn)坐標(biāo)Vector3[] Points,是否可用bool IsUseable
當(dāng)生成器啟用后,計(jì)算所有子物體[路線]的路點(diǎn)坐標(biāo)
當(dāng)生成敵人時(shí),隨機(jī)選擇一條可以使用的路線
代碼實(shí)現(xiàn):
//路線類
public class WayLine
{ public Vector3[] WayPoints{get;set;}
public bool IsUseable{get;set;}
//成員變量聲明完,WayPoints默認(rèn)是null,IsUseable默認(rèn)是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;}}//敵人生成器(腳本給路線點(diǎn))
//腳本在路點(diǎn),怎么找敵人?
public GameObject[] enemyType;//創(chuàng)建的敵人預(yù)制件數(shù)組
public int maxCount=5;//創(chuàng)建敵人最大數(shù)目
public int StartCount=2;
private int enemyCount;//已經(jīng)創(chuàng)建的敵人數(shù)量
//生成一個(gè)敵人
public void GenerateEnemy()
{ //選擇一條可以使用的路線?
//延遲時(shí)間隨機(jī)
//Object.Instantiate(敵人預(yù)制件,位置,旋轉(zhuǎn)角度)=Random.Range(0,enemyType.Length);
//創(chuàng)建敵人
GameObject go=Instantiate(enemyType[randomIndex],路線的第一個(gè)路點(diǎn),Quaternion.identity)as GameObject;
//配置信息
go.GetComponent<EnemyMotor>();
}
using?System.Collections;using?System.Collections.Generic;using?UnityEngine;public?class?EnemySpawn?: MonoBehaviour{public?GameObject[] enemyType;//創(chuàng)建敵人的預(yù)制件public?int?maxCount = 5;public?int?StartCount = 2;//開始同時(shí)創(chuàng)建敵人的數(shù)量private?int?spawnedCount;//已經(jīng)創(chuàng)建敵人數(shù)量private?int?maxDelay = 10;//延遲調(diào)用的最大時(shí)間private?void?Start(){CalculateWayLines();//計(jì)算所有路點(diǎn)//創(chuàng)建敵人for?(int?i = 0; i < StartCount; i++)GenerateEnemy();}private?WayLine[] lines;//路線的數(shù)組是空的private?void?CalculateWayLines(){lines = new?WayLine[this.transform.childCount];//根據(jù)路線數(shù)量創(chuàng)建路線數(shù)組,路線的數(shù)組有了但具體是什么還是空的for(int?i=0;i<lines.Length;i++){//每一個(gè)路線//路線變換組件的引用Transform wayLineTF = this.transform.GetChild(i);//獲取每條路線的路點(diǎn)數(shù)//創(chuàng)建路線對象lines[i] = new?WayLine(wayLineTF.childCount);//lines[0] {F12WayLine(WayPoints:Vector3[4]}//找點(diǎn)的坐標(biāo):把點(diǎn)的坐標(biāo)給到路線的路點(diǎn)里去for(int?pointIndex=0; pointIndex<wayLineTF.childCount; pointIndex++){ ??//給路線路點(diǎn)賦值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();//把集合變?yōu)閿?shù)組返回}public?void?GenerateEnemy(){spawnedCount++;//如果生成數(shù)量,已達(dá)到上限if(spawnedCount >=maxCount)return;//延遲產(chǎn)生一個(gè)敵人Invoke("CreateEnemy", Random.Range(1, maxDelay));}private?void?CreateEnemy(){//選擇一條可以使用的路線?//選擇所有可以使用的路線WayLine[] usableWayLines = SelectUsableWayLine();//隨機(jī)選擇一條WayLine line = usableWayLines[Random.Range(0, usableWayLines.Length)];//延遲時(shí)間隨機(jī)?int?randomIndex = Random.Range(0, enemyType.Length);//創(chuàng)建敵人GameObject go = Instantiate(enemyType[randomIndex], line.WayPoints[0], Quaternion.identity) as?GameObject;//配置信息EnemyMotor motor = go.GetComponent<EnemyMotor>();motor.line = line;//傳遞路線line.IsUseable = false;//傳遞生成器對象引用【建議使用委托代替】(“回掉”為了敵人能調(diào)生成方法,把自己的引用傳遞進(jìn)去)go.GetComponent<EnemyStatusInfo>().spawn = this;}}總結(jié)
以上是生活随笔為你收集整理的Unity—英雄无敌(前方高能)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 腾讯地图位置服务器,腾讯地图推出地形图服
- 下一篇: 东北虎介绍