Unity中Flappy Bird按空格键鸟无响应问题求助及解决
问题与解决:Unity复刻Flappy Bird输入无响应
- 背景:刚入门Unity,跟着教程复刻Flappy Bird,将Player脚本挂载到小鸟对象后,键盘、鼠标/触摸输入均无响应,核对教程步骤、找程序员姐姐排查都未发现问题。
- 已解决:问题根源是未将刚体组件的Body Type设置为Kinematic,调整后输入操作恢复正常。
Player脚本代码
public class Player : MonoBehaviour { private Vector3 direction; public float gravity = -9.8f; public float strength = 5f; private void Update() { if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) { direction = Vector3.up * strength; //我试过加Debug.Log,能正常输出(补充说明) } if (Input.touchCount > 0) { Touch touch = Input.GetTouch(0); if (touch.phase == TouchPhase.Began) { direction = Vector3.up * strength; } } direction.y += gravity * Time.deltaTime; transform.position += direction * Time.deltaTime; } }
内容的提问来源于stack exchange,提问作者NamNam
相关产品推荐
相关产品推荐

