Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Camera Rotation not working

Discussion in 'Editor & General Support' started by Spiral_gamz, Dec 1, 2023.

  1. Spiral_gamz

    Spiral_gamz

    Joined:
    Jan 13, 2022
    Posts:
    2
    Ok so i am a noob to Unity i just started. I wanted to make a simple fnaf 2 type game. i was making the first mechanic, The camera. and i wanted there to be three hallways. one to your right, one in front of you, and one to the left of you. I wanted the camera to rotate of the "A" and "D" keys. However when i rotate right the "A" key does not work. I cant figure this out and chatgpt cant figure this out either. so i am at a loss. Any help would be appreciated.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Runtime.CompilerServices;
    4. using UnityEngine;
    5. using UnityEngine.Animations;
    6. using UnityEngine.InputSystem;
    7. public class CameraScript : MonoBehaviour
    8. {
    9.     public Camera CurrentCamera;
    10.     //private PlayerInputActions playerinput = null;
    11.     private float CamRotation;
    12.     // Start is called before the first frame update
    13.     private void Awake()
    14.     {
    15.         CamRotation = CurrentCamera.transform.rotation.y;
    16.         //CamRotation = CurrentCamera.transform.rotation.y;
    17.         //playerinput = new PlayerInputActions();
    18.         //playerinput.Camera.Enable();
    19.         //playerinput.Camera.MouseX.performed += CheckPos;
    20.  
    21.     }
    22.     private void Update()
    23.     {
    24.         if(CamRotation == 0)
    25.         {
    26.             if (Input.GetKeyDown(KeyCode.A))
    27.             {
    28.                 Quaternion Newangle = Quaternion.Euler(0, -90, 0);
    29.                 CurrentCamera.transform.rotation = Newangle;
    30.                 CamRotation = Newangle.y;
    31.             }
    32.             if (Input.GetKeyDown(KeyCode.D))
    33.             {
    34.                 Quaternion Newangle = Quaternion.Euler(0, 90, 0);
    35.                 CurrentCamera.transform.rotation = Newangle;
    36.                 CamRotation = Newangle.y;
    37.             }
    38.         }
    39.         if(CamRotation == 90)
    40.         {
    41.             if(Input.GetKeyDown(KeyCode.A))
    42.             {
    43.                 Quaternion Newangle = Quaternion.Euler(0, 0, 0);
    44.                 CurrentCamera.transform.rotation = Newangle;
    45.                 CamRotation = Newangle.y;
    46.             }
    47.         }
    48.         if(CamRotation == -90)
    49.         {
    50.             if (Input.GetKeyDown(KeyCode.D))
    51.             {
    52.                 Quaternion Newangle = Quaternion.Euler(0, 0, 0);
    53.                 CurrentCamera.transform.rotation = Newangle;
    54.                 CamRotation = Newangle.y;
    55.             }
    56.         }
    57.     }
    58. }
    59.  
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,317
    I'll suggest something similar to what was discussed here: https://forum.unity.com/threads/why-does-one-rotation-work-while-the-other-doesnt.1521592/

    You can set up some empty game objects rotated to how you want them, and simply Quarternion.RotateTowards (and move the camera if needed too) each of their respective rotations.

    You could reference their transforms in a collection in fact (array or List<T>) and pressing A or D simply increments or decrements this index, and then rotates the camera towards the transform's rotation.

    I'll also note you're falling into the trap of comparing floating point numbers for equality. I would do some reading on floating point precision.

    Last of all, I would look into the Cinemachine package that can do a lot of camera stuff out of the box.
     
  3. Spiral_gamz

    Spiral_gamz

    Joined:
    Jan 13, 2022
    Posts:
    2
    Thanks for the help.