- Home /
Question by
antonsimmerle · Feb 28 at 10:34 AM ·
third-persongame developmentmouselock
Lock Third Person player rotation
Hey guys, I want that my third person character has a lock so he don't rotates over 360°. I hope someone has a solution.
Here's the mouse rotating script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class LockMouse : MonoBehaviour {
public float mouseSensitivity = 500f; public Transform playerBody;
float xRotation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
//float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseX;
xRotation = Mathf.Clamp(-180, xRotation, 180);
playerBody.Rotate(Vector3.up * mouseX);
}
}
Comment
Answer by V_0_1_D_ · Feb 28 at 09:25 PM
Change last two lines like this:
xRotation = Mathf.Clamp(xRotation, -180, 180);
playerBody.rotation = Quaternion.AngleAxis(xRotation, Vector3.up);
Good Lock :)
Your answer
Follow this Question
Related Questions
Using a Key to Activate Camera Mouse Look 1 Answer
rolling ball is a child that needs to be steered by parent 1 Answer
Problem with mouse look and second skybox cam 0 Answers
Does the Mecanim animator automatically put in motion? 1 Answer
Moving 3rd person player relative to orbiting camera 1 Answer