我的代码存在一个问题,不允许我根据鼠标移动向左和向右查看。我可以上下看,但不能左右看。请注意,我确实必须使用 RotateTowards 和 MoveTowards 作为要求的一部分
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>();
// 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;
}
}
我添加了简单的调试行来查看水平鼠标输入(lookInput.x)获得的值。 这是我添加到 Update() 函数中的内容。
// 调试水平外观的鼠标输入值 Debug.Log("鼠标 X: " + LookInput.x);
我得到了这个输出:
看起来 X 轴的鼠标输入已正确注册。
如果您想要做的只是根据鼠标位置乘以速度来向左或向右旋转对象,则只需将欧拉角所需变化的
Vector3
添加到 transform.rotation
的欧拉角分量即可
就像下面的代码应该可以工作。
transform.rotation.eulerAngles += new Vector3(0, LookInput.x * LookSpeed * Time.deltaTime, 0);