暂停菜单帮助Unity

问题描述 投票:0回答:1

我知道这个问题已经被问过很多次了,但我的问题似乎略有不同。进入暂停菜单时,我无法锁定相机旋转。我尝试过查看其他所有类似的问题,但这些都没有帮助。希望这会。我已附上我的脚本。

暂停菜单脚本:

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

public class PauseMenu : MonoBehaviour
{
    public static bool paused = false;
    public GameObject PauseMenuCanvas;
    
    // Start is called before the first frame update
    void Start()
    {
        Time.timeScale = 1f;
    }
    
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (paused)
            {
                Play();
            }
            else
            {
                Stop();
            }
        }
    }
    
    void Stop()
    {
        PauseMenuCanvas.SetActive(true);
        Time.timeScale = 0;
        paused = true;
        Cursor.lockState = CursorLockMode.None; 
    }
    
    public void Play()
    {
        PauseMenuCanvas.SetActive(false);
        Time.timeScale = 1;
        paused = false;
        Cursor.lockState = CursorLockMode.Locked;
    }
    
    void MainMenuButton()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
    }
}

玩家脚本:

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

public class PlayerController : MonoBehaviour {

    [Header("Configurations")]
    public float walkSpeed;
    public float runSpeed;
    public float jumpSpeed;

    [Header("References")]
    public Rigidbody rb;
    public Transform head;
    public Camera camera;

    [Header("Runtime")]
    Vector3 newVelocity;
    bool isGrounded = false;
    bool isJumping = false;

    // Start is called before the first frame update
    void Start(){
        // Hide and lock the mouse cursor
        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    void Update(){
       // Horizontal rotation
       transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * 2f);

       newVelocity = Vector3.up * rb.velocity.y;
       float speed = Input.GetKey(KeyCode.LeftShift) ? runSpeed : walkSpeed;
       newVelocity.x = Input.GetAxis("Horizontal") * speed;
       newVelocity.z = Input.GetAxis("Vertical") * speed;
       
       if (isGrounded) {
           if (Input.GetKeyDown(KeyCode.Space) && !isJumping) {
               newVelocity.y = jumpSpeed;
               isJumping = true;
            }
       }
       rb.velocity = transform.TransformDirection(newVelocity);
    }

    void FixedUpdate() {
        // Shoot a ray of 1 unit towards the ground
        if (Physics.Raycast(transform.position, Vector3.down, out RaycastHit hit, 1f)) {
            isGrounded = true;
        }
        else isGrounded = false;
    }

    void LateUpdate(){
        //Vertical rotation
        Vector3 e = head.eulerAngles;
        e.x -= Input.GetAxis("Mouse Y") *2f;
        e.x = RestrictAngle(e.x, -85f, 85f);
        head.eulerAngles = e;
    }

    void OnCollisionStay(Collision col) {
        if (Vector3.Dot(col.GetContact(0).normal, Vector3.up) <= .6f)
            return;
        
        isGrounded = true;
        isJumping = false;
    }

    //Clamp the vertical head roatation
    public static float RestrictAngle(float angle, float angleMin, float angleMax) {
        if (angle > 180)
            angle -= 360;
        else if (angle < -180)
            angle += 360;

        if (angle > angleMax)
            angle = angleMax;
        if (angle < angleMin)
            angle = angleMin;

        return angle;
    }
}

我尝试查看其他人的问题,但他们都使用内置的 Unity Player。我自己创建了这个 FPS,所以我不知道该怎么办。我正在制作游戏,并且不想更换我的 FPS 控制器,因为它附加了许多其他组件。如果您想了解任何其他信息,请随时询问,但我似乎找不到解决此问题的方法。我知道这与时间尺度和增量时间有关,但我对此很陌生,正在尝试了解更多信息。抱歉添加另一个暂停菜单问题。

c# unity-game-engine game-development
1个回答
0
投票

您有两种方法之一

首先是更改您的播放器脚本以匹配您的新暂停菜单机制,如下所示

void Update(){
   // Horizontal rotation
   if(Time.timeScale!=0)
       transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * 2f);
   .
   .
   .
}
void LateUpdate(){
    if(Time.timeScale==0)
        return 
    //Vertical rotation
    Vector3 e = head.eulerAngles;
    e.x -= Input.GetAxis("Mouse Y") *2f;
    e.x = RestrictAngle(e.x, -85f, 85f);
    head.eulerAngles = e;
}

或者您可以创建另一个根本不移动的摄像机并停用您的游戏摄像机,然后激活另一个摄像机。我会在 PauseMenu 类中管理它们。

我推荐第二个解决方案,但如果你想让它快速工作,只需使用第一个解决方案

© www.soinside.com 2019 - 2024. All rights reserved.