Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

I made a Fly Camera but i need help

Discussion in 'Scripting' started by brayT, Mar 21, 2019.

  1. brayT

    brayT

    Joined:
    Jan 21, 2018
    Posts:
    65
    Hi ladies and Gentlemen i made a Fly camera or what i like to call it a freerom camera and when i made it it works but when i turn around the controls become like inverted like the w is to go forward but then i turn around and when i press the w again it goes backward.

    Here's the script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class freerom_camera : MonoBehaviour
    5. {
    6.     public float flySpeed = 0.5f;
    7.     public GameObject defaultCam;
    8.     public GameObject playerObject;
    9.     private bool isEnabled;
    10.     private bool shift;
    11.     private bool ctrl;
    12.     public float accelerationAmount = 3f;
    13.     public float accelerationRatio = 1f;
    14.     public float slowDownRatio = 0.5f;
    15.     public float speedH = 2.0f;
    16.     public float speedV = 2.0f;
    17.     private float yaw = 0.0f;
    18.     private float pitch = 0.0f;
    19.  
    20.     void Update()
    21.     {
    22.         if (Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyDown(KeyCode.RightShift))
    23.         {
    24.             shift = true;
    25.             flySpeed *= accelerationRatio;
    26.         }
    27.  
    28.         if (Input.GetKeyUp(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.RightShift))
    29.         {
    30.             shift = false;
    31.             flySpeed /= accelerationRatio;
    32.         }
    33.         if (Input.GetKeyDown(KeyCode.LeftControl) || Input.GetKeyDown(KeyCode.RightControl))
    34.         {
    35.             ctrl = true;
    36.             flySpeed *= slowDownRatio;
    37.         }
    38.         if (Input.GetKeyUp(KeyCode.LeftControl) || Input.GetKeyUp(KeyCode.RightControl))
    39.         {
    40.             ctrl = false;
    41.             flySpeed /= slowDownRatio;
    42.         }
    43.         if (Input.GetAxis("Vertical") != 0)
    44.         {
    45.             transform.Translate(-defaultCam.transform.forward * flySpeed * Input.GetAxis("Vertical"));
    46.         }
    47.         if (Input.GetAxis("Horizontal") != 0)
    48.         {
    49.             transform.Translate(-defaultCam.transform.right * flySpeed * Input.GetAxis("Horizontal"));
    50.         }
    51.         if (Input.GetKey(KeyCode.E))
    52.         {
    53.             transform.Translate(defaultCam.transform.up * flySpeed * 0.5f);
    54.         }
    55.         else if (Input.GetKey(KeyCode.Q))
    56.         {
    57.             transform.Translate(-defaultCam.transform.up * flySpeed * 0.5f);
    58.         }
    59.         if (Input.GetKeyDown(KeyCode.F12))
    60.             switchCamera();
    61.         if (Input.GetKeyDown(KeyCode.M))
    62.             playerObject.transform.position = transform.position; //Moves the player to the flycam's position. Make sure not to just move the player's camera.
    63.         yaw += speedH * Input.GetAxis("Mouse X");
    64.         pitch -= speedV * Input.GetAxis("Mouse Y");
    65.         transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
    66.         Cursor.visible = false;
    67.         Cursor.lockState = CursorLockMode.Locked;
    68.     }
    69.  
    70.     void switchCamera()
    71.     {
    72.         if (!isEnabled) //means it is currently disabled. code will enable the flycam. you can NOT use 'enabled' as boolean's name.
    73.         {
    74.             transform.position = defaultCam.transform.position; //moves the flycam to the defaultcam's position
    75.             defaultCam.GetComponent<Camera>().enabled = false;
    76.             this.GetComponent<Camera>().enabled = true;
    77.             isEnabled = true;
    78.         }
    79.         else if (isEnabled) //if it is not disabled, it must be enabled. the function will disable the freefly camera this time.
    80.         {
    81.             this.GetComponent<Camera>().enabled = false;
    82.             defaultCam.GetComponent<Camera>().enabled = true;
    83.             isEnabled = false;
    84.         }
    85.     }
    86. }
    it be nice if you could check it out and explain to me the best you can what is happening and how i can fix it
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    You're inadvertently applying the camera's rotation twice. On this line, for example:
    Code (csharp):
    1. transform.Translate(-defaultCam.transform.forward * flySpeed * Input.GetAxis("Vertical"));
    transform.Translate operates in the object's local space - that is, it takes into account the object's rotation. But you also have .transform.forward there, which again takes into account the object's rotation. If you change the second one of these to Vector3.forward, it should fix that. (Same thing on all the other similar lines)
     
  3. brayT

    brayT

    Joined:
    Jan 21, 2018
    Posts:
    65
    thanks i will see if that works