- Home /
How to freeze the z axis while rotating
I'm trying to make a third person camera but when rotating the z axis it makes it look weird and i don't know how to make it not rotate the z axis here's my code using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CameraController : MonoBehaviour
{
private GameObject player;
private GameObject cam;
public float senstivity;
public float smoothing;
// Use this for initialization
void Start()
{
cam = GameObject.FindGameObjectWithTag("MainCamera");
player = GameObject.FindGameObjectWithTag("Player");
}
// Update is called once per frame
void Update()
{
if (player == null) return;
transform.position = new Vector3(player.transform.position.x, player.transform.position.y + 4, player.transform.position.z);
var mouse = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
var v = new Vector3(-mouse.y, mouse.x, 0) * senstivity * (1 / smoothing);
transform.localRotation *= Quaternion.Euler(v.x, v.y, 0);
}
}
the camera is a child of an gameobject called camera controller which stays on top of the player (because i want the camera to rotate around a point on top of the player)
Have you tried using transform.Rotate() (first version) ins$$anonymous$$d of directly multiplying with a rotation? I know it doesn't make sense, but Transform.Rotate() seems to work better.
Answer by rugvedkhandekar · Mar 25, 2018 at 05:56 PM
Actually as much as I understood reading your question @HamzaNa113, I am not sure you want to freeze the Z axis rotation of the player Camera or a player or a rigidbody. Well, if its a rigid body, you can check the freeze rotation under constraints in the rigid body menu in the inspector.
And I dont know for sure, but you can experiment by applying rigidbody to the camera and enabling the suitable constraints. I hope my answer helped you.
Answer by NoMoneys · Mar 25, 2018 at 06:25 PM
Try changing the order of the variables you're putting into Quaternion.Euler(), that should do it.