unity怪物攻击玩家减血_Unity-塔防游戏之怪物波次简单算法
塔防游戲是非常經典的游戲 它的玩法也比較簡單 一波接著一波的怪物出來 ?怪物沿著固定的路線行走 玩家需要控制攻擊塔在怪物到達終點時 將它消滅 否則 到達終點的怪物超過一定的數量 玩家就輸了
一、每一波的怪物的屬性是不同的 比如說 它的移動速度 生命值、怪物數量等。
二、需要設置一共多小波 每一波出現的時間間隔 每一波怪物間的間隔
讓我們開始吧:
1. 先確定好兩個位置 一個是怪物出現的位置 一個是怪物行走的終點位置
2. 創建怪物預設體 ?給怪物加上控制腳本 代碼如下:usingSystem.Collections;
usingSystem.Collections.Generic;
usingUnityEngine;
usingUnityEngine.AI;// 波次結構體
public struct Wave
{
//波次間隔
public float waveInterval;
//怪物間隔
public float monsterInterval;
//怪物個數
public int monsterCount;
//怪物移動速度
public float monsterSpeed;
//怪物血量
public float monsterHealth;
}public class MonsterInit:MonoBehaviour{
//怪物出生點
publicTransformbegin;
//怪物預設體
public GameObject monsterPrefab;
//波次
public int waveCount=10;
//波次數組
public Wave[] waves;
//可以初始化
private bool canInit=true;
//計數器、計時器
private float waveTimer=0;
private float monsterTimer=0;
private int waveIndex=0;
private int monsterIndex=0;void Awake()
{
// 初始化數組
waves=new Wave[waveCount];
}
思路如下:每一波的怪物會越來越強 那么我我就以第一波為為基礎 然后在循環設置接下來的每一波 每一波在每一波的基礎上去增強屬性。
voidStart()
{
//設置第一波怪的信息
waves[0].waveInterval=5f;
waves[0].monsterInterval=1.5f;
waves[0].monsterCount=5;
waves[0].monsterSpeed=0.3f;
waves[0].monsterHealth=100;
for(inti=1;i
{
waves[i].waveInterval=waves[0].waveInterval-i*0.1f;
waves[i].monsterInterval=waves[0].monsterInterval-i*0.1f;
waves[i].monsterCount=waves[0].monsterCount+i;
waves[i].monsterSpeed=waves[0].monsterSpeed+i*0.2f;
waves[i].monsterHealth=waves[0].monsterHealth+i*10f;
}
}
//Updateiscalledonceperframe
voidUpdate(){
if(!canInit)
return;
waveTimer+=Time.deltaTime;
if(waveTimer>=waves[waveIndex].waveInterval)
{
//如果當期波次的怪物沒有生成完畢
if(monsterIndex
//怪物計時器計時
monsterTimer+=Time.deltaTime;
if(monsterTimer>=waves[waveIndex].monsterInterval){
GameObjectcurrentMonster=Instantiate(monsterPrefab,begin.position,Quaternion.identity);
//設置怪物導航速度
currentMonster.GetComponent().speed=waves[waveIndex].monsterSpeed;
//怪物計數器++
monsterIndex++;
//怪物計時器歸零
monsterTimer=0;
//Debug.Log("初位置"+begin.position);
}
}
else
{
//波次++
waveIndex++;
//怪物計數器歸零
monsterIndex=0;
//波次計時器歸零
waveTimer=0;
//怪物計時器歸零
monsterTimer=0;
if(waveIndex>=waveCount){
canInit=false;
}
}
}
}
}
總結
以上是生活随笔為你收集整理的unity怪物攻击玩家减血_Unity-塔防游戏之怪物波次简单算法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 4、全连接神经网络
- 下一篇: 工业环境中的LED照明降低成本,提高安全