Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

RTS Camera Rotation Problems! New to Scripting

Discussion in 'Scripting' started by Uncharted_Artist, Apr 24, 2014.

  1. Uncharted_Artist

    Uncharted_Artist

    Joined:
    Apr 24, 2014
    Posts:
    3
    Hello Everyone,

    I'm brand new to scripting and it has been both a struggle and a pleasure to learn. I have learned a lot by reading the scripting references and what i have read on this forum, but i am having an issue with my camera control. Everything works exactly how i want it except when i rotate the camera.

    When i rotate the camera my camera moves get all messed up because the engine doesn't reset the cameras axis after the rotation, so even though im dragging my mouse to the top of the screen it doesnt move the camera forward. It moves the camera forwards based off the world axis.

    I know thats what im telling it to do with the "Space.World" input but the reason i need to use that so the engine doesnt move the Camera through the ground when i tell it to move it forward because i have the camera at a 45 degree angle from the top. Maybe i'm not clear in my explanation, but my question is:

    Is there a way to tell it to reset the camera Axis while keeping the current rotation of the camera so when i rotate the camera the axis goes with it and i can then tell it to move along the Y,X,Z axis of the camera instead of the Space.world.

    I really hope i explained this some what clear. Thanks all.

    Here is source code for my Camera Control

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraController : MonoBehaviour {
    5.  
    6.     //Camera Speed Variables
    7.     public float cameraSpeed = 30.0f;
    8.     public float shiftBonus = 90.0f;
    9.  
    10.     //Mouse Screen Threshold
    11.     public float mouseEdgeThreshold = 15.0f;
    12.     public float mousePanEdgeThreshold = 0.0f;
    13.  
    14.     //Camera Position Variables
    15.     public float camPositionLeftMax = -2000.0f;
    16.     public float camPositionRightMax = 2000.0f;
    17.     public float camPositionTopMax = 2000.0f;
    18.     public float camPositionBottomMax = -2000.0f;
    19.     public float camPositionX = 0.0f;
    20.     public float camPositionY = 0.0f;
    21.  
    22.     public float PanSpeed = 1.0f;
    23.  
    24.     // Use this for initialization
    25.     void Start () {
    26.    
    27.     }
    28.    
    29.     // Update is called once per frame
    30.     void Update () {
    31.  
    32.         camPositionX = Camera.main.transform.position.x;
    33.         camPositionY = Camera.main.transform.position.z;
    34.  
    35.    
    36.             //Camera Rotation Controls
    37.  
    38.         if (Input.GetKey (KeyCode.Q)) {
    39.                         transform.Rotate (0, 1, 0, Space.World);
    40.                 }
    41.  
    42.         if (Input.GetKey (KeyCode.E)) {
    43.             transform.Rotate (0, -1, 0, Space.World);
    44.         }
    45.  
    46.         //Camera Horizontal Left Controls
    47.  
    48.         if (camPositionX > camPositionLeftMax) {
    49.                         if (Input.mousePosition.x < mouseEdgeThreshold) {
    50.  
    51.                                 transform.Translate (Vector3.left * cameraSpeed, Space.World);
    52.                         }
    53.                         if ( Input.mousePosition.x < mouseEdgeThreshold  Input.GetKey (KeyCode.LeftShift)) {
    54.                
    55.                                 transform.Translate (Vector3.left * shiftBonus, Space.World);
    56.                         }
    57.  
    58.                         if (Input.GetKey (KeyCode.A)) {
    59.  
    60.                                 Camera.main.transform.Translate (Vector3.left * cameraSpeed, Space.World);
    61.                
    62.                         }
    63.  
    64.                         if (Input.GetKey (KeyCode.A)  Input.GetKey (KeyCode.LeftShift)) {
    65.                
    66.                                 transform.Translate (Vector3.left * shiftBonus, Space.World);
    67.                
    68.                         }
    69.            
    70.         }
    71.  
    72.         //Camera Horizontal Right Controls
    73.  
    74.         if (camPositionX < camPositionRightMax) {
    75.  
    76.                         if (Input.mousePosition.x > Screen.width - mouseEdgeThreshold) {
    77.            
    78.                                 transform.Translate (Vector3.right * cameraSpeed, Space.World);
    79.                         }
    80.  
    81.                         if ( Input.mousePosition.x > Screen.width - mouseEdgeThreshold  Input.GetKey (KeyCode.LeftShift)) {
    82.                
    83.                                 transform.Translate (Vector3.right * shiftBonus, Space.World);
    84.                         }
    85.  
    86.                         if (Input.GetKey (KeyCode.D)) {
    87.                
    88.                                 transform.Translate (Vector3.right * cameraSpeed, Space.World);
    89.                
    90.                         }
    91.  
    92.                         if (Input.GetKey (KeyCode.D)  Input.GetKey (KeyCode.LeftShift)) {
    93.                
    94.                                 transform.Translate (Vector3.right * shiftBonus, Space.World);
    95.                
    96.                         }
    97.         }
    98.  
    99.         //Camera Vertical Bottom Controls
    100.  
    101.         if (camPositionY > camPositionBottomMax) {
    102.        
    103.                         if (Input.mousePosition.y < mouseEdgeThreshold) {
    104.            
    105.                                 transform.Translate (Vector3.back * cameraSpeed, Space.World);
    106.                         }
    107.  
    108.                         if ( Input.mousePosition.y < mouseEdgeThreshold  Input.GetKey (KeyCode.LeftShift)) {
    109.                
    110.                                 transform.Translate (Vector3.back * shiftBonus, Space.World);
    111.                         }
    112.  
    113.                         if (Input.GetKey (KeyCode.S)) {
    114.                
    115.                                 transform.Translate (Vector3.back * cameraSpeed, Space.World);
    116.                
    117.                         }
    118.  
    119.                         if (Input.GetKey (KeyCode.S)  Input.GetKey (KeyCode.LeftShift)) {
    120.                
    121.                                 transform.Translate (Vector3.back * shiftBonus, Space.World);
    122.                
    123.                         }
    124.         }
    125.  
    126.         //Camera Vertical Top Controls
    127.  
    128.         if (camPositionY < camPositionTopMax) {
    129.  
    130.                         if (Input.mousePosition.y > Screen.height - mouseEdgeThreshold) {
    131.            
    132.                                 transform.Translate (Vector3.forward * cameraSpeed, Space.World);
    133.                         }
    134.  
    135.                         if ( Input.mousePosition.y > Screen.height - mouseEdgeThreshold  Input.GetKey (KeyCode.LeftShift)) {
    136.                
    137.                                 transform.Translate (Vector3.forward * shiftBonus, Space.World);
    138.                         }
    139.  
    140.                         if (Input.GetKey (KeyCode.W)) {
    141.                
    142.                                 transform.Translate (Vector3.forward * cameraSpeed, Space.World);
    143.                
    144.                         }
    145.  
    146.                         if (Input.GetKey (KeyCode.W)  Input.GetKey (KeyCode.LeftShift)) {
    147.                
    148.                                 transform.Translate (Vector3.forward * shiftBonus, Space.World);
    149.                
    150.                         }
    151.         }
    152.  
    153.    
    154.     }
    155. }
     
  2. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    449
    Why don't you use Space.Self and keep the height of the camera manually. You can either do so by storing the height-position before your movementcode and then applying it again after the movement has taken place or you go for a new Vector3, do your speedcalculations (Vector3.right * threshold or whatever) and eliminate the heightchange there before you apply it to the translate function.
     
  3. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    Just take out Space.world when you're rotating the camera. This will rotate on its local axis rather than on the world y axis, so when you move it using space.world it will move forward regardless of its local rotation.
     
  4. Uncharted_Artist

    Uncharted_Artist

    Joined:
    Apr 24, 2014
    Posts:
    3
    Thanks for the replies, yeah i know im newb. These didnt solve my issue maybe i can explain it easier. When i rotate the camera as it stands, when i move forward, the camera appears to be moving backwards because i rotated it say 180 degrees because i have it set to Move Forward based of world. If i use Space.Self then because my camera is initially rotated at a 45 degree angle to give it more of a 3d look as opposed to a straight top down, moving by self will move it through my floor.

    So i need the camera to move based of World so it doesnt move through my floor when i tell it to move forward because im looking towards the ground, and when i rotate it i dont want it to make my controls get flipped around... I'll keep digging of course but was hoping to figure this problem out a little quicker as i already spent hours on this ahah. No worries though guys if you can help then great, if not then most likely im not explaining it clear enough,

    Thanks!
     
  5. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    I got what you were saying. My solution should work. Maybe a code example.
    Code (csharp):
    1.  
    2.  
    3.         if (Input.GetKey (KeyCode.Q)) {
    4.  
    5.                         transform.Rotate (0, 1, 0);
    6.  
    7.                 }
    8.  
    9.  
    10.  
    11.         if (Input.GetKey (KeyCode.E)) {
    12.  
    13.             transform.Rotate (0, -1, 0);
    14.  
    15.         }
    16.  
    I know this change should work, since I have functioning code that works that way.
     
  6. mr malee

    mr malee

    Joined:
    Sep 19, 2012
    Posts:
    25
    this should work
    Code (csharp):
    1.  
    2. //project screen center onto a ground plane
    3.  
    4. Ray ray = camera.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2));
    5.  
    6. Plane plane = new Plane(Vector3.up, 0);
    7. float dist;
    8.  
    9. Vector3 p1 = transform.position;
    10. Vector3 p2 = plane.Raycast(ray, out dist) ? ray.GetPoint(dist) : p1;
    11.  
    12. //make sure both points are on the same plane
    13.  
    14. p1.y = p2.y;
    15.  
    16. //get directions
    17.  
    18. Vector3 forward = (p2 - p1).normalized;
    19. Vector3 right = Vector3.Cross(forward, -Vector3.up);
    20.  
    21. //adjust position when keys are pressed
    22.  
    23. if (Input.GetKey(KeyCode.A)) {
    24.  
    25.     transform.position -= right * cameraSpeed;
    26. }
    27.  
    28. if (Input.GetKey(KeyCode.D)) {
    29.  
    30.     transform.position += right * cameraSpeed;
    31. }
    32.  
    33. if (Input.GetKey(KeyCode.S)) {
    34.  
    35.     transform.position -= forward * cameraSpeed;
    36. }
    37.  
    38. if (Input.GetKey(KeyCode.W)) {
    39.  
    40.     transform.position += forward * cameraSpeed;
    41. }
    42.  
     
    Last edited: Apr 25, 2014
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Make your camera a child of an empty GameObject. Use the parent to do rotations instead. This way the rotation stays "flat" and doesn't mess up the downward facing angle of the camera.
     
  8. Uncharted_Artist

    Uncharted_Artist

    Joined:
    Apr 24, 2014
    Posts:
    3
    Dude man!!! It Works! Now comes the task of figuring out why it works. New to C# so there's a lot going on there i don't understand, but thanks a lot mr malee and thanks to everyone who gave up their time to help me out.