【问题标题】:Unity:How to check if area inside camera view?Unity:如何检查相机视图内的区域?
【发布时间】:2017-03-20 03:36:37
【问题描述】:

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

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

敌人生成

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

【问题讨论】:

  • 作为一个简单的解决方案,您可以检查生成位置相对于相机是否为 -z。

标签: unity3d


【解决方案1】:

你可以使用GeometryUtility.TestPlanesAABB来测试点。

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.htmlhttp://answers.unity3d.com/questions/227806/how-do-i-check-if-an-object-is-completely-visible.html

【讨论】:

  • 但我已经有了出生点,我只想检查它们是否在视点内
  • 只需使用IsVisible() 以适当大小的边界测试您的生成点。
猜你喜欢
  • 1970-01-01
  • 2020-09-05
  • 1970-01-01
  • 1970-01-01
  • 2011-01-28
  • 1970-01-01
  • 2017-07-29
  • 2019-06-25
  • 1970-01-01
相关资源
最近更新 更多