簡體   English   中英

在Unity的攝影機邊界之外生成僵屍

[英]Spawning zombies just outside of camera bounds in Unity

通常,在任何新引擎中,我都會嘗試使用簡單的圖形(通常是正方形/矩形)制作自上而下的僵屍射擊游戲,而這正是我目前在Unity中嘗試做的事情。

我要說的是:

  • 射擊的球員(並通過WASD /箭頭鍵和鼠標控制)
  • 產生並走向玩家的僵屍
  • 可以殺死的僵屍(一旦所有僵屍都死了,就會產生另一波浪)

但是,目前看來,我產生它們的方式使它們產生的距離離玩家太遠了。 我使用正交攝影機。

碼:

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

public class ZombieSpawner : MonoBehaviour {
    private int waveNumber = 0;
    public int enemiesAmount = 0;
    public GameObject zombie;
    public Camera cam;
    // Use this for initialization
    void Start () {
        cam = Camera.main;
        enemiesAmount = 0;
    }

// Update is called once per frame
void Update () {
        float height = 2f * cam.orthographicSize;
        float width = height * cam.aspect;
        if (enemiesAmount==0) {
            waveNumber++;
            for (int i = 0; i < waveNumber; i++) {
                Instantiate(zombie, new Vector3(cam.transform.position.x + Random.Range(-width, width),3,cam.transform.position.z+height+Random.Range(10,30)),Quaternion.identity);
                enemiesAmount++;
            }
        }
    }
}

如果要讓它們僅在攝影機視圖之外生成僵屍,請不要乘以正交大小。

float height = cam.orthographicSize; // now zombies spawn od camera view border
float height = cam.orthographicSize + 1 // now they spawn just outside

這是一個很小的變化,但是您也可以將width設置為:

float width = cam.orthographicSize * cam.aspect + 1;

當剩下一個僵屍時,嘗試產生另一波,並觀察游戲節奏如何變化;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM