简体   繁体   English

在Unity的摄影机边界之外生成僵尸

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

Usually in any new engine I try to make a top down zombie shooter using simple graphics (usually squares/rectangles) and that's what I'm currently trying to do in Unity. 通常,在任何新引擎中,我都会尝试使用简单的图形(通常是正方形/矩形)制作自上而下的僵尸射击游戏,而这正是我目前在Unity中尝试做的事情。

I've got to the point where I have: 我要说的是:

  • A player that shoots (and is controlled via WASD/arrow keys and mouse) 射击的球员(并通过WASD /箭头键和鼠标控制)
  • Zombies that spawn and go towards the player 产生并走向玩家的僵尸
  • Zombies that can be killed (and once all zombies are dead, another wave spawns) 可以杀死的僵尸(一旦所有僵尸都死了,就会产生另一波浪)

But, currently, it seems that the way I spawn them spawns them way too far away from the player. 但是,目前看来,我产生它们的方式使它们产生的距离离玩家太远了。 I use an orthographic camera. 我使用正交摄影机。

Code: 码:

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++;
            }
        }
    }
}

If You want them to spawn zombies just outside camera view don't multiply orthographic size. 如果要让它们仅在摄影机视图之外生成僵尸,请不要乘以正交大小。

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

It'a a small change, but You could also set width as: 这是一个很小的变化,但是您也可以将width设置为:

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

Try to spawn another wave when there is one zombie left and see how game pacing has changed ;) 当剩下一个僵尸时,尝试产生另一波,并观察游戏节奏如何变化;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM