在开始游戏之前等待按键?

时间:2018-12-22 01:42:00

标签: c# unity3d

这是我的第一个Untiy3d应用程序,我正在尝试创建一个欢迎屏幕,该屏幕告诉您单击空格以开始游戏,一旦单击空格,则什么也没发生。

这里还有一个小屏幕截图,显示了我在统一编辑器中的选择:http://prntscr.com/ly5o5b

任何帮助将不胜感激。

public class PlayerMovement : MonoBehaviour
{
    public Rigidbody rb;

    public float forwardForce = 2000f;
    public float upwardForce = 0f;
    public float sideForce = 0f;

    public GameObject waitScreen;
    public GameObject mainPlayer;
    public GameObject ingameUI;

    private bool waitingToStartGame = true;


    // Start is called before the first frame update
    void Start()
    {
        mainPlayer.SetActive(false);
        ingameUI.SetActive(false);

        if (waitScreen != null)
        {
            waitScreen.SetActive(true);
        }
        else
        {
            waitingToStartGame = false;
            Debug.LogError("waitScreen was not set in the inspector. Please set and try again");
        }
        if (mainPlayer != null)
        {
            mainPlayer.SetActive(false);
        }
        else
        {
            Debug.LogError("mainPlayer was not set in the inspector. Please set and try again");
        }
    }

    void Update()
    {
        if (waitingToStartGame && (Input.GetKey(KeyCode.Space)))
        {
            waitingToStartGame = false;
            if (waitScreen != null)
            {
                waitScreen.SetActive(false);
            }
            if (mainPlayer != null)
            {
                mainPlayer.SetActive(true);
                ingameUI.SetActive(true);
            }
        }
    }

    void FixedUpdate()
    {
        if (!waitingToStartGame)
        {
            rb.AddForce(sideForce, upwardForce, forwardForce * Time.deltaTime, ForceMode.Force);

            if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
            {
                sideForce = 100f;
                rb.AddForce(sideForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
                Debug.Log("Key D pressed, player moved right");
                sideForce = 0f;
            }

            if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
            {
                sideForce = -100f;
                rb.AddForce(sideForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
                Debug.Log("Key A pressed, player moved left");
                sideForce = 0f;
            }
        }
    }
}

0 个答案:

没有答案