Search Unity

RTS Style Camera moves not correct after rotation

Discussion in 'Scripting' started by GetGeeky, Aug 18, 2019.

  1. GetGeeky

    GetGeeky

    Joined:
    Aug 18, 2019
    Posts:
    2
    Hello, I try to make a simple camera script which can be moved with "WASD" and can be rotated with "QE".
    The movement and rotation work, but when I rotate the camera the movement stays in relation to the world.

    I found several examples on other ways to move the camera but I try to understand what's wrong with my way to do it.

    I think there is an easy fix but I can't put my finger on it.

    Here the Code:

    Code (CSharp):
    1. void LateUpdate()
    2.     {
    3.         MoveCamera();
    4.         RotateCamera();
    5. }
    6.  
    7. private void MoveCamera()
    8. {
    9.     float moveX = Camera.main.transform.position.x;
    10.     float moveY = Camera.main.transform.position.y;
    11.     float moveZ = Camera.main.transform.position.z;
    12.  
    13.     if(Input.GetKey( KeyCode.A))
    14.     {
    15.         moveX -= panSpeed * Time.deltaTime;
    16.     }
    17.     else if (Input.GetKey(KeyCode.D) )
    18.     {
    19.         moveX += panSpeed * Time.deltaTime;
    20.     }
    21.  
    22.     if(Input.GetKey(KeyCode.W))
    23.     {
    24.         moveZ += panSpeed * Time.deltaTime;
    25.     }
    26.     else if (Input.GetKey(KeyCode.S))
    27.     {
    28.         moveZ -= panSpeed * Time.deltaTime;
    29.  
    30.     }
    31.  
    32.  
    33.     // Zoom the camera in and out
    34.     moveY = invertedMouseScroll ? moveY + Input.GetAxis("Mouse ScrollWheel") * (panSpeed): moveY - Input.GetAxis("Mouse ScrollWheel") * (panSpeed);
    35.  
    36.     // Clamp the Zoom Value between Min and Max Height
    37.     moveY = Mathf.Clamp(moveY, minHeight, maxHeight);
    38.  
    39.     Vector3 newPos = new Vector3(moveX, moveY, moveZ);
    40.  
    41.     Camera.main.transform.position = newPos;
    42.  
    43. }
    44.  
    45.     private int RotationDirection
    46.     {
    47.         get
    48.         {
    49.             bool rotateRight = Input.GetKey( KeyCode.Q);
    50.             bool rotateLeft = Input.GetKey( KeyCode.E);
    51.             if (rotateLeft && rotateRight)
    52.                 return 0;
    53.             else if (rotateLeft && !rotateRight)
    54.                 return -1;
    55.             else if (!rotateLeft && rotateRight)
    56.                 return 1;
    57.             else
    58.                 return 0;
    59.         }
    60.     }
    61.  
    62.     private void RotateCamera()
    63.     {
    64.         Vector3 origin = Camera.main.transform.eulerAngles;
    65.         Vector3 destination = origin;
    66.  
    67.         if (Input.GetMouseButton(2)) // Hold midle mouse for rotate
    68.         {
    69.             destination.x -= Input.GetAxis("Mouse Y") * rotateAmount * Time.deltaTime;
    70.             destination.y += Input.GetAxis("Mouse X") * rotateAmount * Time.deltaTime;
    71.         }
    72.  
    73.         if (destination != origin)
    74.         {
    75.             Camera.main.transform.eulerAngles = Vector3.MoveTowards(origin, destination, Time.deltaTime * rotateSpeed);
    76.         }
    77.  
    78.         transform.Rotate(Vector3.up, RotationDirection * Time.deltaTime * rotateSpeed, Space.World);
    79.  
    80.     }
    81.  


    Any tips or comments on how I can change this to work correctly?
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
  3. GetGeeky

    GetGeeky

    Joined:
    Aug 18, 2019
    Posts:
    2
    I already saw that.

    1. Did you mean something like this?
    Code (CSharp):
    1.  newPos = transform.InverseTransformDirection(Vector3.forward);
    2.  
    The Camera is not moving after this, and the rotation is broken. Any Ideas why?

    Option 2 is not the way i want to do this. I want to be able to change the moving keys in a later state of the project.
     
    Last edited: Aug 18, 2019