Unity: Camera Switching with Javascript

What you will learn in this Unity Tutorial

In this Camera Switching tutorial I’ll go over how to create some code with Javascript which, when the user clicks on a button or object, the view changes to the appropriate camera. This can be useful for switching cameras to the user while playing an Unity game.

Unity Javascript

Unity Javascript

Leave Comment

Discussion

17 Responses to “Unity: Camera Switching with Javascript”
  1. Posts: 2
    D B says:

    First!
    Nice, this was much needed.

  2. Posts: 5
    Povilas N says:

    why do you have to type gameObject in Camera.main.gameObeject.active or overs, what it does ?

    • Posts: 109

      I’m actually not 100% sure if this also works by just calling it on a Component directly, but if it does it’s implicit. This way it’s absolutely clear in the code that you’re making the entire GameObject active or non-active; not just a component.

      -Patrick

  3. Posts: 14
    Mike Pegelow says:

    Thanks for the tip!
    The funny thing is, that i thought about it earlier today, if this is possible in unity^^

    • Posts: 109

      Heh, I can tell you know pretty much anything’s possible; it’s almost always a question of “how”, now “if”. ;)

      -Patrick

  4. Posts: 35

    Great Patrick, just what I have been looking for the last couple of days.

    gryff :)

  5. Posts: 2
    Devon H says:

    Hey Patrick, would it be possibly to use one camera and have it lerp to different locations around the scene (to get better transitions)? I mean I know that it is possible, but would you recommend it?
    Thanks in Advance
    -DH

    • Posts: 109

      Hi DH,

      Yes, that’s absolutely a good approach! Lerp(), Slerp() and MoveTowards() are great, light-weight functions for tweening and adding motion. =) Nice catch!

      -Patrick

  6. Posts: 3
    carlos santos says:

    Nice tutorial. cgcookie rocks =)
    Just a quick suggestion. Since unity allows to build for the web, you could put a build of the tutorials to complement the video.

    • Posts: 109

      Hey Carlos,

      Thanks for your suggestion! We’d actually talked about this internally a bit some time ago, but I think it sort of slipped into oblivion. I’ll bring this back up again and see what we can do with this. =)

      -Patrick

  7. Posts: 3

    You can use a function with an index too, I dont know the exact info but I have seen alot of scripts with a index. That way it’s really easy.

  8. Posts: 8

    would you plz make a tut on how to drag and drop a gameobject with the mouse??
    I can’t find a good solution that I understand on the web…

    -bert

  9. Posts: 1
    heebo says:

    Though I would mention here that every camera has to be tagged to “MainCamera”. Spent a lot of time just struggling and wondering what’s wrong with my script, then I noticed that every time you make a camera in Unity it’s tag is “Untagged”.

  10. Posts: 1
    digitaltoon says:

    Is there any upgrade to this tutorial. It’s amazing how hard it is to find a good Camera Switching tutorial like this. I am wondering if there are any ideas how to improve this or use any assets from the unity store to make it easier, faster and better? Thanks so much.

Leave a Comment

You must be logged in to post a comment.