There is an issue with my code that doesn’t allow me to look left and right based on my mouse movement. I can look up and down but i cannot look left and right. Note that i do have to use RotateTowards and MoveTowards as part of the requirements.
Here is my code
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;
}
}
I added simple debug line to see what values im getting for horizontal mouse input (lookInput.x). This is what I added to my Update() function.
// Debug the mouse input values for horizontal look Debug.Log("Mouse X: " + lookInput.x);
and I got this output
It looks like the mouse input for the X-axis is registering correctly.