using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed = 10.0f;
public float gravity = -9.81f;
public float jumpHeight = 3.0f;
public float mouseSensitivity = 100.0f;
public Transform playerBody;
private CharacterController controller;
private Vector3 velocity;
private float xRotation = 0f;
void Start()
{
controller = GetComponent<CharacterController>();
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
Debug.Log("Mouse X: " + mouseX);
Debug.Log("Mouse Y: " + mouseY);
Debug.Log("Local Rotation Before: " + transform.localRotation);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
Debug.Log("Local Rotation After: " + transform.localRotation);
playerBody.Rotate(Vector3.up * mouseX);
if (controller.isGrounded && velocity.y <= 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
if (Input.GetButtonDown("Jump") && controller.isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
Debug.Log("Mouse X: " + mouseX);
Debug.Log("Mouse Y: " + mouseY);
Debug.Log("Local Rotation: " + transform.localRotation);
}
}
Video: https://youtu.be/c2iGfd8AqH0
When I rotate the camera on the Y axis, I see that it rotates and instantly returns to the zero point.
I tried to rewrite the camera movement but now it only rotates along the Y axis. What should I do?
I asked my friends but they themselves don’t know.
New contributor
ertoletik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.