Search Unity

Switching the camera between two objects

Discussion in 'Getting Started' started by MitkoBachvarov, Feb 3, 2016.

  1. MitkoBachvarov

    MitkoBachvarov

    Joined:
    Nov 22, 2015
    Posts:
    5
    Hello !

    In the game that I'm trying to make, I have two characters/heroes but only one character can be active and can move at a time. The other stays inactive.
    I'm doing this by enabling and disabling the Controller scripts on key input.
    I'm switching between the two characters by pressing either " 1 " or " 2 " on the keyboard. Pressing either of those buttons will disable the controller script on the respective character and he won't be able to move.
    Now I want to make the camera to follow the character, which is currently active and I have no idea how to do that...
    Any suggestions ?
    (Suggestions on how to optimise the whole process of switching between hero 1 and hero 2 are welcomed as well)

    Cheers! :)
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Use the CameraFollow script, from the standard assets (probably also on the wiki). And when you switch, just change the target property of that script to the new hero.
     
  3. MitkoBachvarov

    MitkoBachvarov

    Joined:
    Nov 22, 2015
    Posts:
    5
    Thanks for the suggestion I've tried to do it with the CameraFollow script but I couldn't get it to work properly...
    (Note, my coding skills are still noobish, so it's to be expected :) )
    I came up with another solution though: Since I disable and enable the Controller script of each hero, and not the GameObject itself, I created two cameras, each following one of the heroes using the CameraFollow scipt, JoeStrout suggested, and I've added just two lines in both Hero Controllers.
    CameraFollowingHeroOne.SetActive(true);
    CameraFollowingHeroTwo.SetActive(false);
    And vice-versa in the other Controller.

    So when I activate the script for HeroOne, this code is executed and I switch to the camera that follows this hero. Same goes for HeroTwo.