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

Discussion

16 Responses to “Unity: Camera Switching with Javascript”
  1. Posts: 2

    First!
    Nice, this was much needed.

    #
    1
    Feb 28, 2012 at 10:32 am
  2. Posts: 5

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

    #
    2
    Feb 28, 2012 at 10:43 am
    • 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

      #
      2.1
      Feb 28, 2012 at 5:18 pm
  3. Posts: 14

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

    #
    3
    Feb 28, 2012 at 11:16 am
    • Posts: 109

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

      -Patrick

      #
      3.1
      Feb 28, 2012 at 5:19 pm
  4. Posts: 35

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

    gryff :)

    #
    4
    Feb 28, 2012 at 1:42 pm
  5. Posts: 2

    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

    #
    5
    Feb 28, 2012 at 2:39 pm
    • 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

      #
      5.1
      Feb 28, 2012 at 5:22 pm
  6. carlos santos
    Posts: 3

    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.

    #
    6
    Feb 28, 2012 at 8:07 pm
    • 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

      #
      6.1
      Feb 29, 2012 at 7:30 am
  7. 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.

    #
    7
    Mar 6, 2012 at 10:38 am
  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

    #
    8
    Mar 7, 2012 at 8:09 am
  9. love
    Posts: 2

    cool man thx

    #
    9
    Mar 9, 2012 at 9:50 am
  10. heebo
    Posts: 1

    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
    Mar 15, 2012 at 1:44 pm

Leave a Comment

You must be logged in to post a comment.