Unity C#开发Flappy Bird克隆版:管道生成失效问题求助
Flappy Bird管道生成失败问题解决
你的问题出在销毁管道的判断条件上——逻辑写反了。
管道是向左移动的,它的x坐标会越来越小,只有当transform.position.x小于你设置的deadZone(-45)时,才说明管道已经移出屏幕,需要销毁。但你现在写的是:
if (transform.position.x > deadZone) { Destroy(gameObject); }
这意味着只要管道的x坐标还比-45大(刚生成的管道x坐标肯定远大于-45),就会立刻被销毁,所以你看不到任何管道生成。
把判断条件改成小于号就能解决问题:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PipeMoveScript : MonoBehaviour { public float moveSpeed = 5; public float deadZone = -45; void Start() { } void Update() { transform.position = transform.position + (Vector3.left * moveSpeed) * Time.deltaTime; if (transform.position.x < deadZone) { Destroy(gameObject); } } }
另外可以小检查一下:运行游戏时选中管道,在Inspector面板里观察它的x坐标变化,确认deadZone的数值是否符合你的屏幕范围,避免数值设置得不合理导致提前或过晚销毁管道。
内容的提问来源于stack exchange,提问作者A_Programmer
相关产品推荐
相关产品推荐

