Unity:如何检查相机视图内是否有区域? [英] Unity:How to check if area inside camera view?

查看:163
本文介绍了Unity:如何检查相机视图内是否有区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个僵尸生存游戏,在游戏中我有一些游戏对象充当僵尸的生成点.我想在生成点不在相机视图中时生成僵尸,因此如何检查如果生成点在摄影机视图之外以便生成它们.

I am building a zombie survival game, in the game I have some game objects that act as spawn point to the zombies.I want to spawn the zombies when the spawn point is not in the camera view ,so how can I check if the spawn point is outside the camera view so as to spawn them.

下面是我的敌人生成脚本.

Down below is my enemy spawn script.

敌人的出现

public class EnemyManager : MonoBehaviour
{
    PlayerHealth playerHealth;       // Reference to the player's heatlh.
    public GameObject enemy;                // The enemy prefab to be spawned.
    public float spawnTime = 3f;            // How long between each spawn.
    public Transform[] spawnPoints;         // An array of the spawn points this enemy can spawn from.


    void Start ()
    {
        // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
        playerHealth = GameObject.FindWithTag("Player").GetComponent<PlayerHealth>();
        InvokeRepeating ("Spawn", spawnTime, spawnTime);
    }


    void Spawn ()
    {
        // If the player has no health left...
        if(playerHealth.currentHealth <= 0f)
        {
            // ... exit the function.
            return;
        }

        // Find a random index between zero and one less than the number of spawn points.
        int spawnPointIndex = UnityEngine.Random.Range (0, spawnPoints.Length);

        // Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
        Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
    }
}

推荐答案

您可以使用 . TestPlanesAABB 来测试该点. >

You can use GeometryUtility.TestPlanesAABB to test the point.

bool IsVisible(Vector3 pos, Vector3 boundSize, Camera camera)
{
    var bounds = new Bounds(pos, boundSize);
    var planes = GeometryUtility.CalculateFrustumPlanes(camera);
    return GeometryUtility.TestPlanesAABB(planes, bounds);
}

请参见 http://answers.unity3d.com/answers/560147/view. html 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆