Search Unity

how do I make the rotation.z always = 0?

Discussion in 'Scripting' started by Shahar_bar2, Sep 17, 2020.

  1. Shahar_bar2

    Shahar_bar2

    Joined:
    Jun 22, 2020
    Posts:
    6
    well Im starting a new game and I cant understand how am I making my player rotation.z=0.
    here is the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : MonoBehaviour
    6. {
    7.     public float sensetivity = 5;
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.        
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         float mouseY =( Input.GetAxis("Mouse Y") *-1 *sensetivity);
    18.         float mouseX = (Input.GetAxis("Mouse X")* sensetivity);
    19.         transform.Rotate(mouseY,mouseX, 0, 0);
    20.     }
    21. }
     
  2. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
    Code (CSharp):
    1. Vector3 playerRotation = transform.rotation.eulerAngles;
    2. playerRotation.z = 0;
    3. transform.rotation = Quaternion.Euler(playerRotation);
    If you want local rotation instead, replace "transform.rotation" with "transform.localRotation"