- Home /
Can't Stop Rotation along the Z Axis
I am attempting to create a first person camera system, based on a video tutorial I've seen online. However, the issue is that I cannot stop the player character being able to rotate along the Z axis. I have attempted a few different solutions but I have found myself unable to fix the problem. The code appears to ignore the Rigidbody restrictions regarding rotating along the Z axis. I'm guessing its an issue with how the code performs the translation function. Can anyone suggest a way I could fix this? For reference I have added in the code itself as well as a screenshot of the Inspector panel for the player character.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class camMouseCont : MonoBehaviour
{
Vector2 mouseLook;
Vector2 smoothV;
public float sensitivity = 5.0f;
public float ySense = 0.0f;
public float smoothing = 2.0f;
GameObject player;
// Use this for initialization
void Start()
{
//Finds the Game Object that the script is connected to allowing
player = this.transform.gameObject;
Debug.Log(player);
}
// Update is called once per frame
void Update()
{
var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, ySense * smoothing));
smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);
mouseLook += smoothV;
if (mouseLook.y < -30)
{
mouseLook.y = -30;
}
if (mouseLook.y > 30)
{
mouseLook.y = 30;
}
transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
player.transform.localRotation = Quaternion.AngleAxis(-mouseLook.x, player.transform.up);
player.transform.localRotation = Quaternion.AngleAxis(0, player.transform.forward);
}
}
The rigidbody restriction works if you are rotating via Physics but here you are forcing a rotation irrespective of Physics hence the z-rotation is not freezing.
Your answer
Follow this Question
Related Questions
Change of location to move a first person controler 1 Answer
Switch between third and first person controllers. 1 Answer
Compiler Error...Help Needed. 0 Answers
Flight Script throws no errors but does nothing. 2 Answers
Cursor not getting centered 0 Answers