Search Unity

How can I make the camera look at an object? [Cinemachine]

Discussion in 'Timeline' started by gdp2, Feb 22, 2018.

  1. gdp2

    gdp2

    Joined:
    Dec 6, 2016
    Posts:
    25
    Hi,

    I hope this is the right place to post this question. It was the only link to a forum in the Cinemachine documentation. If it's not, I apologize but I would appreciate some help nonetheless.

    I'm trying to use Cinemachine to make my camera look at a particular object in the game. I'm looking for a method like:

    Cinemachine.myCamera.LookAt(someObject);

    Not exactly like that, but hopefully you get the idea. I just need the camera to "focus" on this object and keep it centered on the viewport. From everything that I've read about Cinemachine, this should be completely doable and seems to be a rather primitive task compared to other things that Cinemachine can do. Nonetheless, this functionality is really all that I want from Cinemachine. Nothing fancy.

    Yet, after skimming and re-skimming the docs, I can't find anything which allows me to do this in a simple way. I'd prefer not to create a virtual camera just to do this functionality, but I will do it if it's the only way. However, even if that is the only way, I cannot find a way to do it in the docs. I'm obviously a n00b with Cinemachine so I'd appreciate any pointers.
     
  2. Adam_Myhill

    Adam_Myhill

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    327
    Hi @gdp2 not sure why a link would send you to the Timeline forum

    The Cinemachine forum is here: https://forum.unity.com/forums/cinemachine.136/ .

    Please ask again there if you are still needing help, thanks so much!

    Quick answer - you use virtual cameras, and give it a look at object - anything in Unity works. If you want to look at things with more control - composition, etc - then use the Aim Composer.

    CM works by having potentially lots of vcams, it blends/cuts/mixes them together and applies the product of that to the Main Camera, through the 'Cinemachine Brain' component which is automatically added to the Main Camera when you create your first virtual camera.

    Here's a little tutorial which shows the basics:


    For gameplay you just turn cameras on and off and it will blend between them.

    I suggest checking out some of the many demo scenes included in CM
    upload_2018-2-22_16-31-54.png

    Look how much fun stuff to checkout:
    upload_2018-2-22_16-32-52.png
     
  3. gdp2

    gdp2

    Joined:
    Dec 6, 2016
    Posts:
    25
    Hi Adam,

    Thanks so much for your response. This advice in particular was key for me:

    In order to get the effect I needed, I attached a Brain component to the Main Camera and then added this code in one of my scripts:

    Code (CSharp):
    1. Camera.main.gameObject.transform.Find("CM vcam1").gameObject.GetComponent<CinemachineVirtualCamera>().enabled = false;
    2. Camera.main.gameObject.transform.Find("CM vcam2").gameObject.GetComponent<CinemachineVirtualCamera>().enabled = true;
    Basically, I just switched their enabled states to "blend" between them, as you suggested. I also had to change my "Default Blend", but everything else seems to be working great with Cinemachine. Again, thanks for your advice. Also, sorry for posting on the wrong forum and thanks for pointing me to the right one!
     
  4. gdp2

    gdp2

    Joined:
    Dec 6, 2016
    Posts:
    25
    @Adam_Myhill Hi,

    So everything was going great for a while with my above post, but I need to do a little extra as well. I described my problem here. Would you mind providing some insight?