Search Unity

My code is broke and I need help

Discussion in 'Scripting' started by jamiecropley, May 11, 2021.

  1. jamiecropley

    jamiecropley

    Joined:
    Oct 20, 2014
    Posts:
    46
    Below is my code and a short video showing what the issue is, I know the two lines which are causing the issue but my code has become such a mess I need help mainly condensing it, I think I need to combine stuff here definitely but I am so lost. Some of this code was written by someone in a community I can no longer access due to a bug on there end. It is kinda a mesh of code in a sense so its my own fault for not writing some of it myself, but trying to insert my own stuff has just made it worse.

    The two lines I am having an issue with that are causing the problem in the below video are:

    Code (CSharp):
    1. transform.Rotate(Vector3.up, deltaMousePos.x * rotationSpeed * Time.deltaTime); // HERE IS THE LINE THATS BEING OVERIDDEN BY LINE 345
    and

    Code (CSharp):
    1. transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime * movementTime); //HERE IS OVERRIDING LINE 128
    Also I was trying to work out how to clamp the camera when I rotate it with the right mouse button.

    So overall I am trying to work out clamping of the camera on rotation, and somehow condense the rotations into a single variable? or similar?

    Also I think it would help to clean up my code but how, when I remove something it breaks...




    https://i.gyazo.com/00260af8adb0356c0bcb3251d1364338.mp4

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class CameraController : MonoBehaviour
    5. {
    6.     public Transform CameraTransform;
    7.  
    8.     public float normalSpeed;
    9.     public float fastSpeed;
    10.     public float movementSpeed;
    11.     public float movementTime;
    12.     public float rotationAmount;
    13.     public float speed = 200.0f;
    14.     public int boundary = 1;
    15.     public int width;
    16.     public int height;
    17.  
    18.  
    19.  
    20.  
    21.     public Vector3 zoomAmount;
    22.     public Vector3 newZoom;
    23.     public Vector3 newPosition;
    24.     public Vector3 dragStartPosition;
    25.     public Vector3 dragCurrentPosition;
    26.     public Vector3 rotateStartPosition;
    27.     public Vector3 rotateCurrentPosition;
    28.     public Quaternion newRotation;
    29.  
    30.  
    31.  
    32.     private new Camera camera;
    33.     private Transform camTransform;
    34.     private Vector3 zoomDirection;
    35.     private Vector2 currentMousePos;
    36.     private Vector2 lastMousePos;
    37.     [SerializeField]
    38.     private Vector2 deltaMousePos;
    39.     public Transform xRotTransform;
    40.     public bool isRotating = false;
    41.     public float rotationSpeed = 50.0f;
    42.     public bool isPanning = false;
    43.     public float panSpeed = 10.0f;
    44.     public float zoomSpeed = 250.0f;
    45.  
    46.  
    47.     private void Awake()
    48.     {
    49.         camera = Camera.main;
    50.         camTransform = camera.transform;
    51.         currentMousePos = lastMousePos = Input.mousePosition;
    52.         camTransform.LookAt(transform.position);
    53.         zoomDirection = camTransform.forward;
    54.     }
    55.  
    56.  
    57.     // Start is called before the first frame update
    58.     void Start()
    59.     {
    60.         newPosition = transform.position;
    61.         newRotation = transform.rotation;
    62.         newZoom = CameraTransform.localPosition;
    63.         width = Screen.width;
    64.         height = Screen.height;
    65.     }
    66.  
    67.  
    68.     void FixedUpdate()
    69.     {
    70.  
    71.     }
    72.  
    73.  
    74.  
    75.     // Update is called once per frame
    76.     void Update()
    77.     {
    78.         //HandleMouseInput();
    79.         //HandleMovementInput();
    80.         camTransform.LookAt(transform.position);
    81.         zoomDirection = camTransform.forward;
    82.  
    83.         // Right mouse button.
    84.         if (Input.GetMouseButtonDown(1))
    85.         {
    86.             isRotating = true;
    87.         }
    88.  
    89.         if (Input.GetMouseButtonUp(1))
    90.         {
    91.             isRotating = false;
    92.         }
    93.  
    94.         // Middle mouse button.
    95.         if (Input.GetMouseButtonDown(2))
    96.         {
    97.             isPanning = true;
    98.         }
    99.  
    100.         if (Input.GetMouseButtonUp(2))
    101.         {
    102.             isPanning = false;
    103.         }
    104.  
    105.         MousePositionMovement();
    106.         Rotate();
    107.         Pan();
    108.         Zoom();
    109.  
    110.  
    111.     }
    112.  
    113.  
    114.     private void MousePositionMovement()
    115.     {
    116.         currentMousePos = Input.mousePosition;
    117.         deltaMousePos = currentMousePos - lastMousePos;
    118.         lastMousePos = currentMousePos;
    119.     }
    120.  
    121.     private void Rotate()
    122.     {
    123.         if (!isRotating)
    124.             return;
    125.  
    126.         transform.Rotate(Vector3.up, deltaMousePos.x * rotationSpeed * Time.deltaTime); // HERE IS THE LINE THATS BEING OVERIDDEN BY LINE 345
    127.         xRotTransform.Rotate(Vector3.right, deltaMousePos.y * rotationSpeed * Time.deltaTime);
    128.  
    129.     }
    130.  
    131.     private void Pan()
    132.     {
    133.         if (!isPanning)
    134.             return;
    135.  
    136.  
    137.         var goalPos = transform.position + Vector3.left * deltaMousePos.x + Vector3.back * deltaMousePos.y;
    138.         transform.position = Vector3.Lerp(transform.position, goalPos, panSpeed * Time.deltaTime);
    139.     }
    140.  
    141.     private void Zoom()
    142.     {
    143.         var goalPos = camTransform.position + zoomDirection * Input.mouseScrollDelta.y;
    144.         camTransform.position = Vector3.Lerp(camTransform.position, goalPos, zoomSpeed * Time.deltaTime);
    145.     }
    146.  
    147.  
    148.  
    149.     //The below in LateUpdate() runs after everything in Update() has been called.
    150.     void LateUpdate()
    151.     {
    152.         HandleMouseInput();
    153.         HandleMovementInput();
    154.     }
    155.  
    156.  
    157.     void HandleMouseInput()
    158.     {
    159.  
    160.         Camera.main.fieldOfView += Input.mouseScrollDelta.y * -10f;
    161.  
    162.  
    163.  
    164.  
    165.         /*
    166.         if (Input.mouseScrollDelta.y != 0)
    167.         {
    168.             newZoom += Input.mouseScrollDelta.y * zoomAmount;
    169.         }
    170.         */
    171.         /*
    172.         if (Input.mouseScrollDelta.y > 0)
    173.         {
    174.             newZoom += zoomAmount * Time.deltaTime * 1250f;
    175.         }
    176.  
    177.         if (Input.mouseScrollDelta.y < 0)
    178.         {
    179.             newZoom -= zoomAmount * Time.deltaTime * 1250f;
    180.         }*/
    181.  
    182.  
    183.         if (Input.GetMouseButtonDown(2))
    184.         {
    185.             Plane plane = new Plane(Vector3.up, Vector3.zero);
    186.  
    187.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    188.  
    189.             float entry;
    190.  
    191.             if (plane.Raycast(ray, out entry))
    192.             {
    193.                 dragStartPosition = ray.GetPoint(entry);
    194.             }
    195.         }
    196.  
    197.         if (Input.GetMouseButton(2))
    198.         {
    199.             Plane plane = new Plane(Vector3.up, Vector3.zero);
    200.  
    201.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    202.  
    203.             float entry;
    204.  
    205.             if (plane.Raycast(ray, out entry))
    206.             {
    207.                 dragCurrentPosition = ray.GetPoint(entry);
    208.  
    209.                 newPosition = transform.position + dragStartPosition - dragCurrentPosition;
    210.             }
    211.         }
    212.  
    213.  
    214.  
    215.         if (Input.GetMouseButton(0))
    216.         {
    217.             Debug.Log("Left mouse button held down!");
    218.         }
    219.  
    220.  
    221.         if (Input.GetMouseButton(1))
    222.         {
    223.             Debug.Log("Right mouse button held down!");
    224.         }
    225.  
    226.  
    227.  
    228.         if (Input.GetMouseButton(2))
    229.         {
    230.             Debug.Log("Middle mouse button held down!");
    231.         }
    232.  
    233.  
    234.         if (Input.GetMouseButton(1))
    235.         {
    236.  
    237.             // Mouse rotation here with right mouse button
    238.  
    239.         }
    240.  
    241.  
    242.  
    243.  
    244.  
    245.  
    246.         if (Input.mousePosition.x > width - boundary)
    247.         {
    248.             newPosition -= new Vector3(Input.GetAxisRaw("Mouse X") * Time.deltaTime * -speed, 0.0f, 0.0f);
    249.             //Debug.Log(Input.mousePosition);
    250.         }
    251.  
    252.         if (Input.mousePosition.x < 0 + boundary)
    253.         {
    254.             newPosition -= new Vector3(Input.GetAxisRaw("Mouse X") * Time.deltaTime * -speed, 0.0f, 0.0f);
    255.             //Debug.Log(Input.mousePosition);
    256.         }
    257.  
    258.         if (Input.mousePosition.y > height - boundary)
    259.         {
    260.             newPosition -= new Vector3(0.0f, 0.0f, Input.GetAxisRaw("Mouse Y") * Time.deltaTime * -speed);
    261.             //Debug.Log(Input.mousePosition);
    262.         }
    263.  
    264.         if (Input.mousePosition.y < 0 + boundary)
    265.         {
    266.             newPosition -= new Vector3(0.0f, 0.0f, Input.GetAxisRaw("Mouse Y") * Time.deltaTime * -speed);
    267.             //Debug.Log(Input.mousePosition);
    268.         }
    269.  
    270.     }
    271.  
    272.  
    273.  
    274.     void HandleMovementInput()
    275.     {
    276.         if (Input.GetKey(KeyCode.LeftShift))
    277.         {
    278.             movementSpeed = fastSpeed;
    279.         }
    280.         else
    281.         {
    282.             movementSpeed = normalSpeed;
    283.         }
    284.  
    285.         if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
    286.         {
    287.             newPosition += (transform.forward * movementSpeed);
    288.         }
    289.  
    290.         if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
    291.         {
    292.             newPosition += (transform.forward * -movementSpeed);
    293.         }
    294.  
    295.         if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
    296.         {
    297.             newPosition += (transform.right * movementSpeed);
    298.         }
    299.  
    300.         if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
    301.         {
    302.             newPosition += (transform.right * -movementSpeed);
    303.         }
    304.  
    305.  
    306.  
    307.         if (Input.GetKey(KeyCode.Q))
    308.         {
    309.             newRotation *= Quaternion.Euler(Vector3.up * rotationAmount);
    310.  
    311.         }
    312.  
    313.         if (Input.GetKey(KeyCode.E))
    314.         {
    315.             newRotation *= Quaternion.Euler(Vector3.up * -rotationAmount);
    316.         }
    317.  
    318.  
    319.         if (Input.GetKey(KeyCode.Z))
    320.         {
    321.             newRotation *= Quaternion.Euler(Vector3.forward * rotationAmount);
    322.  
    323.         }
    324.  
    325.         if (Input.GetKey(KeyCode.C))
    326.         {
    327.             newRotation *= Quaternion.Euler(Vector3.forward * -rotationAmount);
    328.         }
    329.  
    330.  
    331.  
    332.         if (Input.GetKey(KeyCode.R))
    333.         {
    334.             newZoom += zoomAmount;
    335.         }
    336.  
    337.         if (Input.GetKey(KeyCode.F))
    338.         {
    339.             newZoom -= zoomAmount;
    340.         }
    341.  
    342.         transform.position = Vector3.Lerp(transform.position, newPosition, Time.deltaTime * movementTime);
    343.         transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime * movementTime); //HERE IS OVERRIDING LINE 128
    344.         CameraTransform.localPosition = Vector3.Lerp(CameraTransform.localPosition, newZoom, Time.deltaTime * movementTime);
    345.     }
    346.  
    347.  
    348. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Camera controllers are difficult beasts to work with under the best of circumstances.

    You may want to consider switching to Cinemachine (package from Unity) in order to handle camera stuff. It can basically do probably just about anything you want it to.
     
  3. jamiecropley

    jamiecropley

    Joined:
    Oct 20, 2014
    Posts:
    46
    I tried Cinemachine and decided not to go with it. It made my code worse if anything unfortunately.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    If your code is a Camera Controller (I infer this from the class name) , then Cinemachine would make your code go away, as in Casper, like gone. That's the point of Cinemachine: you don't have to write annoying camera code.

    IF your code above actually does something MORE than a Camera Controller, then it's probably time to refactor it and keep separation of concerns in mind:

    https://en.wikipedia.org/wiki/Separation_of_concerns
     
    Lurking-Ninja likes this.
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,934
    Kurt-Dekker likes this.
  6. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    i agree i check cinemachine too and better stick with my own code , i dont like "bad" documentation of cinemachine it is just not same as unity , different property names in code then in inspector , one component in inspector have like 5 components in code etc..
     
  7. jamiecropley

    jamiecropley

    Joined:
    Oct 20, 2014
    Posts:
    46
    I think I am going to try Cinemachine, however I am struggling to find a tutorial that achieves a camera the same as it is done in Stellaris for cinemachine, so far I am finding cinemachine the hardest way of achieving the correction of my two lines of code that I am having issues with, but if the whole community only uses cinemachine then I guess I have no choice to use that instead. Thanks for the help. Update: I found this
    but it looks very 2D
     
    Last edited: May 13, 2021