作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在构建一个僵尸生存游戏,在游戏中我有一些游戏对象充当僵尸的生成点。我想在生成点不在相机 View 中时生成僵尸,那么我该如何检查如果生成点在摄像机 View 之外以便生成它们。
下面是我的敌人生成脚本。
敌怪生成
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);
}
}
最佳答案
您可以使用 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.html和 http://answers.unity3d.com/questions/227806/how-do-i-check-if-an-object-is-completely-visible.html
关于unity3d - 统一:How to check if area inside camera view?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42895445/
我是一名优秀的程序员,十分优秀!