Can i move my minimap camera to the position of the current active game camera?

Hi all,

Is there a way to make my minimap camera move to the location of my active game camera if i have several cameras in the scene?
I only have one camera (apart from the minimap camera) active at any one time. I’m hoping there is some way I can make it flip between locations as I cycle through the cameras. This is the script that i currently have attached to the game object that holds all my cameras:

var selectedCamera : int = 0;
    function Start () 
    {
        SelectCamera (0);
    }
 
    function Update () 
    {
        if (Input.GetKeyDown("x")) 
        {
            selectedCamera++;
            if(selectedCamera == transform.childCount){
            selectedCamera = 0;
            }
            SelectCamera (selectedCamera);
 
        }
        if (Input.GetKeyDown("v"))
		{
		    selectedCamera--;
		    if(selectedCamera < 0){
		    selectedCamera =  transform.childCount;
		    }
		    SelectCamera (selectedCamera);
 
}
        
     }
 
    function SelectCamera (index : int) 
    {
        for (var i=0;i<transform.childCount;i++)    
        {
            if (i == index){
                transform.GetChild(i).gameObject.SetActiveRecursively(true);
            }
            else{
                transform.GetChild(i).gameObject.SetActiveRecursively(false);
            }
        }
    }

Any help greatly appreciated.

2 Answers

2

Well, every camera would of course need a boolean variable that tracks wether the camera is active or not. This way, you’ll know to which camera the minimap camera should jump. You could also store the currently active camera in a gameObject variable. Other than that, I am notsure where you problem is :slight_smile:

Hi, Thanks for the reply, but i'm not really sure what you mean? I have all my cameras working as i want, and i have them all stored inside an empty game object so that they cycle through correctly. The activating and deactivating of the game cameras is working fine. The problem i am having is that i then have 1 minimap camera that is not linked to anything, and i'd like that camera to move to the position above where the active camera is so that my minimap position jumps from one location to another. Sorry if my earlier explanation was a little unclear.

I wonder, is there a way i can adapt the Vector3.SmoothDamp to do this. If i attach this code to my minimap camera it moves to where the first person controller is. However if i add a second variable it doesn’t seem to move. This is the code i tried adding to my minimap camera:

	// Smooth towards the target
	var target : Transform;
	var target1 : Transform;
	var smoothTime = 0.3;
	private var velocity = Vector3.zero;
	
	function Update () {
		// Define a target position above and behind the target transform
		var targetPosition : Vector3 = target.TransformPoint(Vector3(0, 5, -10));
		
		// Smoothly move the camera towards that target position
		transform.position = Vector3.SmoothDamp(transform.position, targetPosition,
		                             velocity, smoothTime);

		// Define a target position above and behind the target transform
		var target1Position : Vector3 = target1.TransformPoint(Vector3(0, 5, -10));
		
		// Smoothly move the camera towards that target position
		transform.position = Vector3.SmoothDamp(transform.position, target1Position,
		                             velocity, smoothTime);		                             
		                             
		                             
	}