首页 > 解决方案 > 如何让相机的垂直旋转使相机上下移动?

问题描述

我在游戏中有一个玩家,我可以用键盘移动它,用鼠标只能在水平轴上旋转。也就是说,我只能水平瞄准,不能上下瞄准。

我有 Cinemachine 的主摄像头和另一个 VM 摄像头。游戏目前的状态是这样的:

在此处输入图像描述

在水平轴上,我旋转玩家,但在垂直轴上,我只希望玩家的相机/FOV 上下移动。

我附加到播放器的移动脚本是:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class PlayerMovement : MonoBehaviour
{
    public CharacterController characterController;
    public float speed = 35f;
    public Animator animator;
 
    // camera and rotation
    public Transform cameraHolder;
    public float mouseSensitivity = 2f;
    public float upLimit = 50;
    public float downLimit = -50;
 
    // gravity
    private float gravity = 9.87f;
    private float verticalSpeed = 0;
 
 
    void Update()
    {
        Move();
        Rotate();
    }
 
 
    public void Rotate()
    {
        float horizontalRotation = Input.GetAxis("Mouse X");
        float verticalRotation = Input.GetAxis("Mouse Y");
 
        transform.Rotate(0, horizontalRotation * mouseSensitivity, 0);
        cameraHolder.Rotate(-verticalRotation * mouseSensitivity, 0, 0);
 
        Vector3 currentRotation = cameraHolder.localEulerAngles;
        if (currentRotation.x > 180) currentRotation.x -= 360;
        currentRotation.x = Mathf.Clamp(currentRotation.x, upLimit, downLimit);
        cameraHolder.localRotation = Quaternion.Euler(currentRotation);
    }
 
    private void Move()
    {
        float horizontalMove = Input.GetAxis("Horizontal");
        float verticalMove = Input.GetAxis("Vertical");
 
        if (characterController.isGrounded) verticalSpeed = 0;
        else verticalSpeed -= gravity * Time.deltaTime;
        Vector3 gravityMove = new Vector3(0, verticalSpeed, 0);
 
        Vector3 move = transform.forward * verticalMove + transform.right * horizontalMove;
        characterController.Move(speed * Time.deltaTime * move + gravityMove * Time.deltaTime);
    }
}

标签: c#unity3dcameramouse

解决方案


这是我使用的代码,它对我有用,它非常容易实现,并且当你不专注于游戏时停止移动相机,是为此目的修改的 Brackey 教程之一中的脚本:

using UnityEngine;

public class CameraController : MonoBehaviour
{
    public PlayerController player;
    public float sensitivity = 150f;
    public float clampAngle = 85f;
    public bool look = true;

    private float verticalRotation;
    private float horizontalRotation;

    private void Start()
    {
        verticalRotation = transform.localEulerAngles.x;
        horizontalRotation = player.transform.eulerAngles.y;

        // Defines the state of the cursor
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    private void Update()
    {
        // Looks around if the user is in the window
        if (look)
        {
            Look();
        }
        Debug.DrawRay(transform.position, transform.forward * 2, Color.red);

        // If the player presses ESC while in the game, it unlocks the cursor
        if (look && Input.GetKeyDown(KeyCode.Escape))
        {
            look = false;
            Cursor.lockState = CursorLockMode.None;
            Cursor.visible = true;
        }
        else if (Input.GetMouseButtonDown(0) && !look)
        {
            look = true;
            Cursor.lockState = CursorLockMode.Locked;
            Cursor.visible = false;
        }

    }

    private void Look()
    {
        float _mouseVertical = -Input.GetAxis("Mouse Y");
        float _mouseHorizontal = Input.GetAxis("Mouse X");

        verticalRotation += _mouseVertical * sensitivity * Time.deltaTime;
        horizontalRotation += _mouseHorizontal * sensitivity * Time.deltaTime;

        verticalRotation = Mathf.Clamp(verticalRotation, -clampAngle, clampAngle);

        transform.localRotation = Quaternion.Euler(verticalRotation, 0f, 0f);
        player.transform.rotation = Quaternion.Euler(0f, horizontalRotation, 0f);
    }
}

推荐阅读