Assinging Multiple Camaras to Multiple points.

Hey Guys,

Right todays silly question…

I have a ship which has multiple camara points, all made as a single prefab of an EmptyGameObject with a simple instantiate script attached.

I then have several camaras also made as prefabs with various camara scripts attached (I need each camara to have its own behavior or/and restrictions, hence why im using several camara prefabs).

at the moment my Instantiate script is a public variable so I have to drag the camara in. oviously this is great for testing, but won’t work later on.

My question is what is the best way of spawning the correct Camara prefab on to the correct EmptyGameObject Prefab (or camara position on the ship) in code?

Would an array do the job?

I only ask so I don’t spend many hours learning stuff which won’t actually do the job I require of it…im an artist and don’t exacly have the patients of some of you crazy programmer types :wink:

Many thanks for any advise.

John

If you’re setting up the ship as a prefab and need each camera configured individually anyway, why not just include the configured cameras (with their scripts) in the ship’s prefab to start with? Is there some specific reason that you have to add them separately later on?

LOL,fair point well made!

talk about making something more complex than it needs to be :slight_smile:

The idea is that im going to have alot of ships (and aircraft actually), each with a similar number of camera points and positions, so ill have to make sure I put an Awake type function in the Camara scripts, so any particular camera on the currently selected ship activates when the player goes to it, either via the GUI or a hotkey, so you don’t have hundreds of unused cameras ticking away at your system resources.

laying the foundations for at least a prototype Naval RTS :slight_smile:

Cheers for the Tip AngryP.

John

So I guess that the idea is that when a ship is selected one or more of its cameras will start showing ship-specific stuff?

Will each ship have an equivalent set of cameras? And should they all turn on/off at once, or will different cameras be used for different things?

basically the game should be played on a 2d map, thats how it could be done, just like that, but since im an artist I like my 3d stuff and cinematic style shizzle so wish to implement some nice visuals as well. :slight_smile:

therefore when the player has a ship selected in the 2d map, they can go to a 3d view of the ship and watch the action take place in the 3d world…thats the theory anyway.

Yep, each ship will have equivalent set of cameras, however the position of the cameras will vary slightly (due to the different sizes and types of ship).
I don’t want all cameras being active at the same time because if every ship Type prefab active in the scene has the camaras positioned this would be a massive waste of resources, so id need to create a function that activates a specific camera on a specific ship when the player hits the relevant hotkey(or GUI button).

John

Does this help??

/
/
/
var camera1 : Camera; 

var camera2 : Camera; 

var camera3 : Camera;

var camera4 : Camera;

public var startCamera : int = 1;

 

function Start () 

{ 

      camera1.enabled = true; 

      camera2.enabled = false; 

      camera3.enabled = false;

      camera4.enabled = false;

      startCamera = 1;

} 

function Update () 

{ 

   if (Input.GetKeyDown ("c")  (startCamera == 1))

    { 

      startCamera = 2;

      camera1.enabled = false; 

      camera2.enabled = true; 

      camera3.enabled = false;

      camera4.enabled = false;

    } 

    else if (Input.GetKeyDown ("c")  (startCamera == 2))

    { 

      startCamera = 3;

      camera1.enabled = false; 

      camera2.enabled = false; 

      camera3.enabled = true;

      camera4.enabled = false;

    } 

    else if (Input.GetKeyDown ("c")  (startCamera == 3))

    { 

      startCamera = 4;

      camera1.enabled = false; 

      camera2.enabled = false; 

      camera3.enabled = false;

      camera4.enabled = true;

    } 

    else if (Input.GetKeyDown ("c")  (startCamera == 4))

    { 

      startCamera = 1;

      camera1.enabled = true; 

      camera2.enabled = false; 

      camera3.enabled = false;

      camera4.enabled = false;

    } 

}
/
/
/

Aaaaahhhh… why multiple cameras… your only using one.

consider using an array of transforms and just set the main camera to the current camera position…

var cameraNodes : Transform[];
var cam : int = 0;

function Start(){
	if(!cameraNodes){
		cameraNodes = new Transform[1];
		cameraNodes[0] = Camera.main.transform;
	}
	SetCamera();
}

function Update(){
	if(Input.GetKeyDown(KeyCode.C)){
		cam = (cam + 1) % cameraNodes.Length;
		SetCamera();
	}
}

function SetCamera(){
	Camera.main.position = cameraNodes[i].position;
	Camera.main.rotation = cameraNodes[i].rotation;
}

And… far less code.

Thanks Big, that is alot better…

Getting an error on the …

On the Bright side I actually understood All Americans version…

On the downside it makes more sense to use Bigs version. I understood most of it but not all…apart from the error on which I presume is because its not been declared anywhere. Ive also tried translating this to C#, which is the language im working in.
*

[quote]*
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CameraNodes : MonoBehaviour {

  • public List cameraNodes;*

  • public int cam = 0;*

  • // Use this for initialization*

  • void Start () {*

  • if(!cameraNodes){ //ERROR HERE The ‘!’ makes it a Bool in C# which is not allowed as CameraNodes is an array what should I put instead?*

  • cameraNodes = new List(1);*

  • cameraNodes[0] = Camera.main.transform;*

  • }*

  • SetCamera();*

  • }*

  • // Update is called once per frame*

  • void Update () {*

  • if (Input.GetKeyDown(KeyCode.C))*

  • {*

  • cam = (cam + 1) % cameraNodes.Count;*

  • SetCamera();*

  • }*

  • }*

  • private void SetCamera(){*
    _ Camera.main.position = cameraNodes*.position; //ERROR Here ‘i’ is not declared anywhere._
    _Camera.main.rotation = cameraNodes.rotation;
    }
    }
    [/QUOTE]*
    couple of questions -
    1. In java what is the % sign mean, is literally a %? (swift google search came up with very little suprisingly)
    2. To assign this script presumably id just make an EmptyGameObject in the world and attach it to that in order to get it initialising properly?
    3. presumably I still set-up the camera prefabs as discussed previously in this thread and position each camera in my Ship prefab?
    Many Thanks for the help.
    John_

    *[/quote]

Yeah, if you’re only actually going to be rendering from one camera at a time, you may as well only have one camera and move it between GameObjects/Transforms as buggested by Big.

The % sign is “modulo”, so that should help you look it up.

@All_American, instead of a bunch of functions with mostly copied code, I’d stick the cameras in an array, take in an index, and then use a for loop on the array to enable or disable cameras based on their index. This is similar to what Big is doing, except he’s moving one camera rather than toggling between many.

@Big, I’d extend on that a little further by making the camera’s transform a child of the specified Transform, then setting its local position and rotation to zero. That way it’ll move with the object it’s attached to.