我正在嘗試更改我在網上找到的這個腳本https://pressstart.vip/tutorials/2018/09/25/58/spawning-obstacles.html以使用正交相機,因為這是我的游戲使用的。目前它只適用于透視相機,我真的不知道它是如何作業的,因為我還沒有真正接觸過相機矩陣。這是腳本的代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class deployAsteroids : MonoBehaviour {
public GameObject asteroidPrefab;
public float respawnTime = 1.0f;
private Vector2 screenBounds;
// Use this for initialization
void Start () {
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
StartCoroutine(asteroidWave());
}
private void spawnEnemy(){
GameObject a = Instantiate(asteroidPrefab) as GameObject;
a.transform.position = new Vector2(screenBounds.x * -2, Random.Range(-screenBounds.y, screenBounds.y));
}
IEnumerator asteroidWave(){
while(true){
yield return new WaitForSeconds(respawnTime);
spawnEnemy();
}
}
}
我的目標是更改腳本以使其在正交相機上正常作業。(縮進搞砸了,這不是問題)。
uj5u.com熱心網友回復:
出于某種原因,代碼Camera.main.transform.position.z用于相機深度組件。在 2d 游戲中的典型相機設定中,該值將是負值。
所以它通過跟隨相機后面的截錐體的右邊緣來找到螢屏的左側。很奇怪。如果它是正交的,你不能沿著截錐體的右側找到螢屏左側的位置,所以它不起作用也就不足為奇了。
相反,只需使用截錐體的左側并通過否定該組件使深度為正:
screenBounds = Camera.main.ViewportToWorldPoint(
new Vector3(0f, 0f, -Camera.main.transform.position.z));
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/449570.html
下一篇:如何將2D物件統一旋轉90度
