Код: Выделить всё
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();
playerCamera = GetComponentInChildren();
// 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 lookInput = lookAction.action.ReadValue();
// 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() > 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;
}
}
Это то, что я добавил в свою функцию Update().
Я добавил простую строку отладки, чтобы увидеть, какие значения я получаю при горизонтальном вводе с помощью мыши (lookInput.x).
Это то, что я добавил в свою функцию Update().
p>
// Отладка значений ввода мыши для горизонтального просмотра
Debug.Log("Mouse X: " +lookInput.x);
I получил этот вывод:

Похоже, что ввод мыши по оси X регистрируется правильно.
Подробнее здесь: https://stackoverflow.com/questions/790 ... y-in-unity
Мобильная версия