【发布时间】:2021-02-04 16:20:32
【问题描述】:
我正在尝试让我的游戏对象(小行星)在游戏中不断生成。我查找并按照本教程进行操作:https://pressstart.vip/tutorials/2018/09/25/58/spawning-obstacles.html。为了让小行星移动,我创建了这个脚本(AsteroidObject)并生成了对象,我创建了这个脚本(DeployAsteroids)。没有错误,Debug.Log 出现在控制台中。但是小行星游戏对象是看不到的,也不会产生。任何人都可以帮忙吗?提前致谢!
小行星目标代码:
public class AsteroidObject : MonoBehaviour
{
public float speed = 10.0f; //how fast the asteroid will move
private Rigidbody2D rb;
private Vector2 screenBounds; //screenbounds calculation
// Start is called before the first frame update
void Start()
{
rb = this.GetComponent<Rigidbody2D>(); //find rigidbody 2d and set it to rb reference by using the getcomponent
rb.velocity = new Vector2(-speed, 0); //moving the asteroid from right to left by setting the x value, leaving the y value 0 so that it will not move up and down
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z)); //defines the boundaries of the screen on the x and y axis
}
// Update is called once per frame
void Update()
{
//transform.position = new Vector3(Mathf.Clamp(transform.position.x, -9f, 9f),
//Mathf.Clamp(transform.position.y, -4f, 4f), transform.position.z);
if (transform.position.x < screenBounds.x) //check if it is moving to the left of the screen
{
Destroy(this.gameObject);
Debug.Log("hello world");
}
}
}
部署小行星代码:
public class DeployAsteroids : MonoBehaviour
{
public GameObject asteroidPrefab;
public float respawnTime = 1.0f;
private Vector2 screenBounds;
// Start is called before the first frame update
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();
//Debug.Log("Hello World");
}
}
}
【问题讨论】:
-
请谨慎标记您的问题。
Visual-Studio与您的问题完全无关,请注意unityscript是一种 JavaScript 风格,类似于早期 Unity 版本中使用的自定义语言,现在早已弃用!你的脚本显然是c# -
主摄像头是透视模式还是正交模式?