You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity相机无法水平旋转问题(需使用RotateTowards和MoveTowards)

第一人称控制器左右视角旋转失效问题

我的第一人称控制器代码存在问题:无法通过鼠标移动实现左右视角旋转,但上下视角查看功能正常。要求必须使用RotateTowards和MoveTowards方法实现相关逻辑。

我已添加调试代码验证鼠标X轴输入,确认输入值能正常读取,但问题仍未解决。完整代码如下:

using UnityEngine;
using UnityEngine.InputSystem;

public class FirstPersonController : MonoBehaviour
{
    // Input actions
    [SerializeField] private InputActionReference moveAction;
    [SerializeField] private InputActionReference lookAction;
    [SerializeField] private InputActionReference sprintAction;

    // Character controller and camera
    private CharacterController characterController;
    private Camera playerCamera;
    private Transform cameraTarget;  // Camera target for vertical rotation

    // Movement and rotation settings
    private float moveSpeed = 5f;
    private float sprintSpeed = 8f;   // Speed while sprinting
    private float rotationSpeed = 100f;
    private float lookSpeed = 2f;
    private float verticalRotation = 0f;

    public MazeManager mazeManager; // Reference to the MazeManager script

    private void Awake()
    {
        characterController = GetComponent<CharacterController>();
        playerCamera = GetComponentInChildren<Camera>();

        // Create an empty object to act as the camera target and position it above the player
        cameraTarget = new GameObject("CameraTarget").transform;
        cameraTarget.parent = transform; // Attach it to the player
        cameraTarget.localPosition = new Vector3(0, 1.5f, 0); // Adjust the height as needed
    }

    private void OnEnable()
    {
        moveAction.action.Enable();
        lookAction.action.Enable();
        sprintAction.action.Enable();
    }

    private void OnDisable()
    {
        moveAction.action.Disable();
        lookAction.action.Disable();
        sprintAction.action.Disable();
    }

    private void Start()
    {
        if (mazeManager != null)
        {
            transform.position = mazeManager.GetMazeEntrancePosition();
        }
        else
        {
            Debug.LogError("MazeManager is not assigned!");
        }
    }

    private void Update()
    {
        // Read input values
        Vector2 moveInput = moveAction.action.ReadValue<Vector2>();
        Vector2 lookInput = lookAction.action.ReadValue<Vector2>();

        // 调试代码:验证鼠标X轴输入
        Debug.Log("Mouse X: " + lookInput.x);

        // Handle horizontal rotation using RotateTowards
        float mouseX = lookInput.x * lookSpeed;
        Quaternion targetRotation = Quaternion.Euler(0f, transform.eulerAngles.y + mouseX, 0f);
        transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);

        // Handle vertical rotation using the camera target
        verticalRotation -= lookInput.y * lookSpeed; // Inverted mouse Y movement for vertical look
        verticalRotation = Mathf.Clamp(verticalRotation, -80f, 80f); // Clamp to avoid extreme up/down look
        cameraTarget.localRotation = Quaternion.Euler(verticalRotation, 0f, 0f);

        // Ensure the camera looks at the camera target to follow the vertical rotation
        playerCamera.transform.position = cameraTarget.position; // Match the target position
        playerCamera.transform.localRotation = cameraTarget.localRotation; // Match the target rotation

        // Handle sprint action - Check if sprint (shift key) is pressed
        bool isSprinting = sprintAction.action.ReadValue<float>() > 0f;

        // If sprint is pressed, use the sprint speed
        float currentMoveSpeed = isSprinting ? sprintSpeed : moveSpeed;

        // Calculate movement direction
        Vector3 forward = playerCamera.transform.forward;
        forward.y = 0f; // Ignore vertical component
        forward.Normalize();

        Vector3 right = playerCamera.transform.right;
        right.y = 0f; // Ignore vertical component
        right.Normalize();

        // Calculate movement direction based on input
        Vector3 moveDirection = (forward * moveInput.y + right * moveInput.x).normalized;

        // Move the player using MoveTowards
        Vector3 newPosition = Vector3.MoveTowards(transform.position, transform.position + moveDirection, currentMoveSpeed * Time.deltaTime);
        characterController.Move(newPosition - transform.position); // Apply movement
    }

    // Method to change movement speed
    public void SetMoveSpeed(float newSpeed)
    {
        moveSpeed = newSpeed;
    }
}

调试输出显示鼠标X轴输入值正常,但左右视角旋转功能仍无响应。


内容的提问来源于stack exchange,提问作者Joe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 11:03:15