- Home /
 
 
               Question by 
               SrLuquitas · Mar 04, 2021 at 08:13 PM · 
                camera2drtscamera follow  
              
 
              How to make the camera start in the camera2 position (Unity 2D)
Hi, im making a game with RTS-type camera, and i basically have 2 cameras, one that follow the player (camera2) and a free cam (camera) , i want the free cam to start in the same position that the following camera, but it keeps in the position when you stop using it, how can i solve this problem? Here are the 2 camera scripts:
 public class CameraController : MonoBehaviour
 {
     public Camera camera;
     public Camera camera2;
     public float offset;
     public float speed;
     //x - min y - max
     public Vector2 minMaxXPosition;
     public Vector2 minMaxYPosition;
     private float screenWidth;
     private float screenHeight;
     private Vector3 cameraMove;
 
    
     // Use this for initialization
     void Start()
     {
         camera.enabled = true;
         camera2.enabled = false;
 
         screenWidth = Screen.width;
         screenHeight = Screen.height;
         cameraMove.x = transform.position.x;
         cameraMove.y = transform.position.y;
         cameraMove.z = transform.position.z;
 
         
     }
     // Update is called once per frame
     void Update()
     {
 
         //Move camera
         if ((Input.mousePosition.x > screenWidth - offset) && transform.position.x < minMaxXPosition.y)
         {
             cameraMove.x += MoveSpeed();
         }
         if ((Input.mousePosition.x < offset) && transform.position.x > minMaxXPosition.x)
         {
             cameraMove.x -= MoveSpeed();
         }
         if ((Input.mousePosition.y > screenHeight - offset) && transform.position.y < minMaxYPosition.y)
         {
             cameraMove.y += MoveSpeed();
         }
         if ((Input.mousePosition.y < offset) && transform.position.y > minMaxYPosition.x)
         {
             cameraMove.y -= MoveSpeed();
         }
         transform.position = cameraMove;
 
         if (Input.GetKeyUp(KeyCode.Space))
         {
             camera.enabled = !camera.enabled;
             camera2.enabled = !camera2.enabled;
         }
         
 
     }
 
     float MoveSpeed()
     {
         return speed * Time.deltaTime;
     }
 
               And the Camera2:
 public class Camera2Controller : MonoBehaviour
 {
     public Transform target;
 
     public Camera camera;
     public Camera camera2;
 
     //x - min y - max
     public Vector2 maxPosition;
     public Vector2 minPosition;
 
     // Start is called before the first frame update
     void Start()
     {
         camera.enabled = true;
         camera2.enabled = false;
     }
 
     // Update is called once per frame
     void Update()
     {
         Vector3 targetPos = new Vector3(target.position.x, target.position.y, transform.position.z);
 
         targetPos.x = Mathf.Clamp(targetPos.x, minPosition.x, maxPosition.x);
 
         targetPos.y = Mathf.Clamp(targetPos.y, minPosition.y, maxPosition.y);
 
         transform.position = targetPos;
     }
 }
 
               PD: Sorry for the english, im from Argentina :P
               Comment
              
 
               
              Your answer