Search Unity

Question Camera Z axis rotating even tho it shouldn't

Discussion in 'Scripting' started by getmyisland_dev, Sep 16, 2021.

  1. getmyisland_dev

    getmyisland_dev

    Joined:
    Dec 22, 2020
    Posts:
    100
    Here is a video that shows my problem:



    When I move my mouse down the cameras z Axis rotates, which should not happen. Is there something that can cause this kind of problem.

    As long as I know there is no collider attached to the camera. Only the movement script, post processing and of course the camera component.

    I hope someone can help me and thanks in advance.
     
  2. Cameron_SM

    Cameron_SM

    Joined:
    Jun 1, 2009
    Posts:
    915
    Show the code doing the camera rotation.

    When stuff like this happens it helps to draw debug lines along the axes you think things are moving - can help you find code that you intended to do X but is actually doing Y.

    Also, I think the [BUG] tag is only for when you've identified a bug in Unity itself and can show a reproduction case. Not necessarily for when you have a bug in your own code - cause that's 99% of the posts here.
     
    Last edited: Sep 17, 2021
    getmyisland_dev and Yoreki like this.
  3. getmyisland_dev

    getmyisland_dev

    Joined:
    Dec 22, 2020
    Posts:
    100
    Sorry for the late answer I hope someone can still help me.

    The full code of my camera control script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraControl : MonoBehaviour
    6. {
    7.     private float speed = 20.0f;
    8.     float edgeSize = 40f;
    9.  
    10.     private void Update()
    11.     {
    12.         if (!this.enabled || !CameraManager.instance.watchingCams || CameraManager.instance.cameraPanelOpen)
    13.             return;
    14.  
    15.         if (Input.mousePosition.x > Screen.width - edgeSize)
    16.         {
    17.             //Edge Right
    18.             transform.RotateAround(this.transform.position, Vector3.up, speed * Time.deltaTime);
    19.         }
    20.  
    21.         if (Input.mousePosition.x < edgeSize)
    22.         {
    23.             //Edge Left
    24.             transform.RotateAround(this.transform.position, Vector3.up, -speed * Time.deltaTime);
    25.         }
    26.  
    27.         if(Input.mousePosition.y > Screen.height - edgeSize)
    28.         {
    29.             //Edge Up
    30.             transform.RotateAround(this.transform.position, Vector3.right, -speed * Time.deltaTime);
    31.         }
    32.  
    33.         if(Input.mousePosition.y < edgeSize)
    34.         {
    35.             //Edge Down
    36.             transform.RotateAround(this.transform.position, Vector3.right, speed * Time.deltaTime);
    37.         }
    38.     }
    39. }
    40.  
    I changed the code so when you are near an edge you rotate, because I think its better for the game I'm making, anyway the "bug" still exists.

    When you get near the top edge or the bottom edge it rotates the cameras Z axis and the cameras Y axis. Normally it should only rotate the Y axis.

    I have no clue why this happens, maybe someone can help me. Thank you
     
  4. Cameron_SM

    Cameron_SM

    Joined:
    Jun 1, 2009
    Posts:
    915
    transform.RotateAround is not what you want here because you're rotating the transform through the World X and Y axes (Vector3.up and Vector3.down).

    This is the same as using Unity's rotation tool in global mode, it doesn't care what the local rotation of the object is, it will rotate it based on the global X/Y/Z axes.

    What you need to do it modify the rotation angles directly like so:

    Code (CSharp):
    1. private void Update()
    2.     {
    3.         var mousePos = Input.mousePosition;
    4.         var rotationSpeed = speed * Time.deltaTime;
    5.         if (mousePos.x > Screen.width - edgeSize)
    6.         {
    7.             var rot = transform.rotation.eulerAngles;
    8.             rot.y += rotationSpeed;
    9.             transform.rotation = Quaternion.Euler(rot);
    10.         }
    11.         else if (mousePos.x < edgeSize)
    12.         {
    13.             var rot = transform.rotation.eulerAngles;
    14.             rot.y -= rotationSpeed;
    15.             transform.rotation = Quaternion.Euler(rot);
    16.         }
    17.  
    18.         if (mousePos.y > Screen.height - edgeSize)
    19.         {
    20.             var rot = transform.rotation.eulerAngles;
    21.             rot.x += rotationSpeed;
    22.             transform.rotation = Quaternion.Euler(rot);
    23.         }
    24.         else if (mousePos.y < edgeSize)
    25.         {
    26.             var rot = transform.rotation.eulerAngles;
    27.             rot.x -= rotationSpeed;
    28.             transform.rotation = Quaternion.Euler(rot);
    29.         }
    30.     }
     
    getmyisland_dev likes this.