Search Unity

RTS Camera tutorial!

Discussion in 'Community Learning & Teaching' started by Wikened, May 24, 2013.

  1. Wikened

    Wikened

    Joined:
    Oct 31, 2011
    Posts:
    271
    Enjoy!



    Source Code for any problems:
    Code (csharp):
    1.  
    2. public class RTSCameraHandler : MonoBehaviour {
    3.    
    4.     public int CameraSpeed = 40;
    5.  
    6.     bool _moveForward = false;
    7.     bool _moveBackward = false;
    8.     bool _moveRight = false;
    9.     bool _moveLeft = false;
    10.    
    11.     void Update()
    12.     {
    13.         RTSCamera();   
    14.     }
    15.    
    16.     void RTSCamera()
    17.     {
    18.         if(Input.GetKeyDown(KeyCode.W)){ _moveForward = true; }
    19.         if(Input.GetKeyUp(KeyCode.W)){ _moveForward = false; }
    20.        
    21.         if(Input.GetKeyDown(KeyCode.S)){ _moveBackward = true; }
    22.         if(Input.GetKeyUp(KeyCode.S)){ _moveBackward = false; }
    23.        
    24.         if(Input.GetKeyDown(KeyCode.D)){ _moveRight = true; }
    25.         if(Input.GetKeyUp(KeyCode.D)){ _moveRight = false; }
    26.        
    27.         if(Input.GetKeyDown(KeyCode.A)){ _moveLeft = true; }
    28.         if(Input.GetKeyUp(KeyCode.A)){ _moveLeft = false;}
    29.        
    30.         MoveCamera();  
    31.     }
    32.    
    33.     void MoveCamera()
    34.     {
    35.         if(_moveForward == true){transform.Translate(Vector3.forward * CameraSpeed * Time.deltaTime,Space.World);}
    36.        
    37.         if(_moveBackward == true){transform.Translate(Vector3.back * CameraSpeed * Time.deltaTime,Space.World);}
    38.        
    39.         if(_moveRight == true){transform.Translate(Vector3.right * CameraSpeed * Time.deltaTime,Space.World);}
    40.        
    41.         if(_moveLeft == true){transform.Translate(Vector3.left * CameraSpeed * Time.deltaTime,Space.World);}
    42.     }
    43.  
    44. }
    45.  
     
unityunity