在没有相机移动的情况下暂停菜单

时间:2018-03-10 19:58:10

标签: unity3d

本周我开始使用Unity和C#进行编程,我遇到了一个小问题,我无法让相机停在暂停菜单中。我设法停止时间,但相机仍在移动。我尝试使用youtube中的一些代码,但它们不起作用:/这是我的PlayerMovement和Pause Men脚本,它有一些抛光字,但大多数是高度,速度等klawiatura = keybord和mysz = mouse。

PlayerMovement.cs

using UnityEngine;
using System.Collections;

public class PlayerControler : MonoBehaviour
{

    //Obiekt odpowiedzialny za ruch gracza.
    public CharacterController characterControler;

    public float predkoscPoruszania = 9.0f;
    public float wysokoscSkoku = 7.0f;
    public float aktualnaWysokoscSkoku = 0f;
    public float predkoscBiegania = 7.0f;
    public static bool GameIsPaused = false;        
    public float czuloscMyszki = 3.0f;
    public float myszGoraDol = 0.0f;
    public float zakresMyszyGoraDol = 90.0f;

    void Start()
    {
     characterControler = GetComponent<CharacterController>();
    }
    void Update()
    {
        klawiatura();
        myszka();

    }

    private void klawiatura()
    {

        float rochPrzodTyl = Input.GetAxis("Vertical") * predkoscPoruszania;

        float rochLewoPrawo = Input.GetAxis("Horizontal") * predkoscPoruszania;

        if (characterControler.isGrounded && Input.GetButton("Jump"))
        {
            aktualnaWysokoscSkoku = wysokoscSkoku;
        }
        else if (!characterControler.isGrounded)
        {
            aktualnaWysokoscSkoku += Physics.gravity.y * Time.deltaTime;
        }

        Debug.Log(Physics.gravity.y);

        if (Input.GetKeyDown("left shift"))
        {
            predkoscPoruszania += predkoscBiegania;
        }
        else if (Input.GetKeyUp("left shift"))
        {
            predkoscPoruszania -= predkoscBiegania;
        }
        Vector3 ruch = new Vector3(rochLewoPrawo, aktualnaWysokoscSkoku, rochPrzodTyl);
        ruch = transform.rotation * ruch;
        characterControler.Move(ruch * Time.deltaTime);
    }

    private void myszka()
    {
         if (!GameIsPaused)
            {
            float myszLewoPrawo = Input.GetAxis("Mouse X") * czuloscMyszki;
            transform.Rotate(0, myszLewoPrawo, 0);

            myszGoraDol -= Input.GetAxis("Mouse Y") * czuloscMyszki;
            myszGoraDol = Mathf.Clamp(myszGoraDol, -zakresMyszyGoraDol, zakresMyszyGoraDol);
            Camera.main.transform.localRotation = Quaternion.Euler(myszGoraDol, 0, 0);
            }
    }

}

** PauseMenu.cs ** 我从YouTube上获得了这个并稍微改了一下!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;


public class PauseMenu : MonoBehaviour {

    public static bool GameIsPaused = false;
    public GameObject pauseMenuUI;

    private void Start()
    {
        Resume();
    }
    // Update is called once per frame
    void Update () {
        if (Input.GetKeyDown(KeyCode.Escape))
          {
            if (GameIsPaused)
            {
                Resume();
            }
            else
            {
                Pause();
            }
        }
    }

    public void Resume()
    {
        Cursor.visible = false;
        pauseMenuUI.SetActive(false);
        Time.timeScale = 1f;
        GameIsPaused = false;
    }


   void Pause()
    {
        Cursor.visible = true;
        pauseMenuUI.SetActive(true);
        Time.timeScale = 0f;
        GameIsPaused = true;

    }

    public void LoadMenu()
    {
        SceneManager.LoadScene("menu6");
    }

    public void QuitMenu()
    {
        Application.Quit();
    }


}

编辑:

所以我在第一个脚本中更改了鼠标部分,但它仍然无效。

private void myszka()
{
    if (!GameIsPaused)
    {
        float myszLewoPrawo = Input.GetAxis("Mouse X") * czuloscMyszki;
        transform.Rotate(0, myszLewoPrawo, 0);

        myszGoraDol -= Input.GetAxis("Mouse Y") * czuloscMyszki;
        myszGoraDol = Mathf.Clamp(myszGoraDol, -zakresMyszyGoraDol, zakresMyszyGoraDol);
        Camera.main.transform.localRotation = Quaternion.Euler(myszGoraDol, 0, 0);
    }
}

1 个答案:

答案 0 :(得分:0)

在更新功能中移动相机之前,您需要检查是否!PauseMenu.GameIsPaused。这样,如果游戏暂停,相机将只接收移动命令。

private void myszka()
{
    if (!PauseMenu.GameIsPaused)
    {
        float myszLewoPrawo = Input.GetAxis("Mouse X") * czuloscMyszki;
        transform.Rotate(0, myszLewoPrawo, 0);

        myszGoraDol -= Input.GetAxis("Mouse Y") * czuloscMyszki;
        myszGoraDol = Mathf.Clamp(myszGoraDol, -zakresMyszyGoraDol, zakresMyszyGoraDol);
        Camera.main.transform.localRotation = Quaternion.Euler(myszGoraDol, 0, 0);
    }
}