Fract
Fract

Reputation: 369

How to change cameras in Unity based on camera coordinates?

I'd like to create a movie in Unity, so I would need several cameras and camerapaths. On the top of this off course I'd like to change between them. For example if Camerapath1 reaches a significant point with Camera1 then I'd like to change to Camerapath2 with Camera2, etc.

I also have Camera Path Animator asset installed. It's working perfectly when I'm using it with only one camera for several camerapaths but I'm unable to change between maincameras.

I'm a newcomer to Unity. I also know that I should do something like this:

...
camera1.camera.active = false; 
camera2.camera.active = true;
...

...but where should I populate these lines? On the top of this, how may I catch the event when a camera on a specific camerapath reaches a particular point?

Upvotes: 1

Views: 302

Answers (1)

Everts
Everts

Reputation: 10701

The way to go would be an animation controller that has all camera as children and controls the active state of all cameras. This provides perfect control over the behaviour.

Create an empty game object, add all cameras as children, add an Animator to the main object with one animation. This animation takes all the camera and set their active state. One extra bonus of this approach is the possibility to call methods as well using the AnimationEvent process. You can still define within the animation for some triggered actions like explosions or movements of objects.

As I said, this give you perfect control since you can define easily actions at specific time.

The downside of it is the rigidity of the process. It may not be as flexible as code, but since you are making a movie, you probably do not need flexibility.

If so, you would have your cameras with a collider and rigidbody (isKinematic true), then you would have some trigger box with a simple script:

public void CameraTrigger:MonoBehaviour{
    public GameObject nextCamera;
    void OnTriggerEnter(Collider col){
        if(col.gameObject.CompareTag("Camera")){ 
              col.gameObject.SetActive(false);
              nextCamera.SetActive(true);
        }
    }
}

Then you drag the camera meant to start next as nextCamera.

Upvotes: 1

Related Questions