Search Unity

Isometric RTS camera

Discussion in 'Assets and Asset Store' started by enyllief, Jul 23, 2012.

  1. enyllief

    enyllief

    Joined:
    Oct 17, 2010
    Posts:
    36
    How to make a simple isometric RTS camera?

    You just need to

    1) create an empty game object EyeSocket (Game Object -> Create Empty)
    - set its position to (X, Y, Z) 0, 130, 0


    2) create EyeMovement Javascript script and attach it to EyeSocket


    3) add camera object called Eye (Create Other -> Camera) and attach it to EyeSocket
    - set its Rotation to (X, Y, Z) 30, 45, 0
    - set its Projection to Orthographic
    - set its Size to 50

    <- rotation coordinates should be exactly the same

    EyeMovement.js
    Code (JavaScript):
    1. function Update () {
    2.            
    3.     /////////////////////
    4.     //keyboard scrolling
    5.    
    6.     var translationX : float = Input.GetAxis("Horizontal");
    7.     var translationY : float = Input.GetAxis("Vertical");
    8.     var fastTranslationX : float = 2 * Input.GetAxis("Horizontal");
    9.     var fastTranslationY : float = 2 * Input.GetAxis("Vertical");
    10.    
    11.     if (Input.GetKey(KeyCode.LeftShift))
    12.         {
    13.         transform.Translate(fastTranslationX + fastTranslationY, 0, fastTranslationY - fastTranslationX);
    14.         }
    15.     else
    16.         {
    17.         transform.Translate(translationX + translationY, 0, translationY - translationX);
    18.         }
    19.  
    20.     ////////////////////
    21.     //mouse scrolling
    22.    
    23.     var mousePosX = Input.mousePosition.x;
    24.     var mousePosY = Input.mousePosition.y;
    25.     var scrollDistance : int = 5;
    26.     var scrollSpeed : float = 70;
    27.  
    28.     //Horizontal camera movement
    29.     if (mousePosX < scrollDistance)
    30.         //horizontal, left
    31.         {
    32.         transform.Translate(-1, 0, 1);
    33.         }
    34.     if (mousePosX >= Screen.width - scrollDistance)
    35.         //horizontal, right
    36.         {
    37.         transform.Translate(1, 0, -1);
    38.         }
    39.  
    40.     //Vertical camera movement
    41.     if (mousePosY < scrollDistance)
    42.         //scrolling down
    43.         {
    44.         transform.Translate(-1, 0, -1);
    45.         }
    46.     if (mousePosY >= Screen.height - scrollDistance)
    47.         //scrolling up
    48.         {
    49.         transform.Translate(1, 0, 1);
    50.         }
    51.    
    52.     ////////////////////
    53.     //zooming
    54.     var Eye : GameObject = GameObject.Find("Eye");
    55.    
    56.     //
    57.     if (Input.GetAxis("Mouse ScrollWheel") > 0  Eye.camera.orthographicSize > 4)
    58.         {
    59.         Eye.camera.orthographicSize = Eye.camera.orthographicSize - 4;
    60.         }
    61.    
    62.     //
    63.     if (Input.GetAxis("Mouse ScrollWheel") < 0  Eye.camera.orthographicSize < 80)
    64.         {
    65.         Eye.camera.orthographicSize = Eye.camera.orthographicSize + 4;
    66.         }
    67.  
    68.     //default zoom
    69.     if (Input.GetKeyDown(KeyCode.Mouse2))
    70.         {
    71.         Eye.camera.orthographicSize = 50;
    72.         }
    73.        
    74. }
    EyeSocket + Eye as a prefab, and EyeMovement.js attached. Please note that you need to set EyeSocket position to (X, Y, Z) 0, 130, 0 to get it to move properly.

    View attachment $isometric_rts_camera.unitypackage
     
    Last edited: Jan 21, 2018
  2. Kashif-Wajid

    Kashif-Wajid

    Joined:
    Jul 23, 2012
    Posts:
    1
    nice work. thanks :)
     
  3. samsmithnz

    samsmithnz

    Joined:
    Oct 21, 2012
    Posts:
    13
    How to I limit the mouse movements? On the keyboard I was able to set horizontal and vertical limits so that you couldn't scroll too far off my scene, but I can't seem to figure out how to do this with the mouse. Here is my code:

    Code (csharp):
    1.  
    2.     private float leftSide = -100f;
    3.     private float rightSide = -80f;
    4.     private float topSide = -80f;
    5.     private float bottomSide = -100f;
    6.    
    7.     // Update is called once per frame
    8.     void Update ()
    9.     {
    10.    
    11.         /////////////////////
    12.         //keyboard scrolling       
    13.         float translationX = Input.GetAxis("Horizontal");
    14.         float translationZ = Input.GetAxis("Vertical");
    15.            
    16.         float toX = translationX + translationZ;           
    17.         if (transform.position.x + toX < leftSide)
    18.         {
    19.             toX = 0;
    20.         }
    21.         if (transform.position.x + toX > rightSide)
    22.         {
    23.             toX = 0;
    24.         }
    25.         float toZ = translationZ - translationX;           
    26.         if (transform.position.z + toZ > topSide)
    27.         {
    28.             toZ = 0;
    29.         }
    30.         if (transform.position.z + toZ < bottomSide)
    31.         {
    32.             toZ = 0;
    33.         }
    34.         transform.Translate(toX, 0, toZ);
    35.        
    36.        
    37.         ////////////////////
    38.         //mouse scrolling  
    39.         float mousePosX = Input.mousePosition.x;
    40.         float mousePosY = Input.mousePosition.y;
    41.         int scrollDistance = 1;
    42.         float scrollSpeed = 70;
    43.        
    44.         //Horizontal camera movement
    45.         //horizontal, left
    46.         if (mousePosX < scrollDistance)
    47.         {
    48.             transform.Translate(-1, 0, 1);
    49.         }
    50.         //horizontal, right
    51.         if (mousePosX >= Screen.width - scrollDistance)
    52.         {
    53.             transform.Translate(1, 0, -1);
    54.         }
    55.        
    56.         //Vertical camera movement
    57.         //scrolling down
    58.         if (mousePosY < scrollDistance)
    59.         {
    60.             transform.Translate(-1, 0, -1);
    61.         }
    62.         //scrolling up
    63.         if (mousePosY >= Screen.height - scrollDistance)
    64.         {
    65.             transform.Translate(1, 0, 1);
    66.         }
    67. }
    68.  
     
  4. DiegoC

    DiegoC

    Joined:
    Jan 14, 2011
    Posts:
    36
    Hi,
    How I rotate the camera, please?
    Thanks
     
  5. devstudent14

    devstudent14

    Joined:
    Feb 18, 2014
    Posts:
    133
    //rotate
    if (Input.GetMouseButton(2))
    {
    translationX = Input.GetAxis ("Mouse X");
    transform.RotateAround(Vector3.up, translationX * cameraSpeed * Time.deltaTime);

    }
     
  6. devstudent14

    devstudent14

    Joined:
    Feb 18, 2014
    Posts:
    133
    this only works in c#. You will have to define camera speed to determine how quickly it will rotate.

    I have a question though. I converted your code into c# and it works great but I do not know how to change the camera speed on the zoom or x/y translations.
     
  7. mrgarcialuigi

    mrgarcialuigi

    Joined:
    Feb 11, 2015
    Posts:
    12
    This is a RTS camera I'have created some time ago. Maybe it can help someone.
    [Post]
    [Github link]
     
  8. devstudent14

    devstudent14

    Joined:
    Feb 18, 2014
    Posts:
    133
    Looks like a solid camera. Thanks for the script :) I haven't quite mastered rotation yet so this should help a lot.