Search Unity

Re-Centering Camera on Object

Discussion in 'Scripting' started by Jeffey_101, Jun 7, 2019.

  1. Jeffey_101

    Jeffey_101

    Joined:
    Jul 12, 2018
    Posts:
    21
    I was wondering how exactly I would code my camera so that when C is pressed, the camera returns to an object no matter where that object is (i.e. a continuously moving object). I am trying to code a program where you can view how planets move relative to other celestial bodies, and sometimes you may lose a planet. Any help would be appreciated.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,749
    You can use the transform.LookAt() command to look at a position or another transform. The camera transform is particularly useful to aim this way.

    In order to regard your target from a reasonable distance away, you'll probably want to preserve some notion of how far back the camera should be given the field of view and size of the object involved.

    Also, you'll probably want to tween the regard point and the camera point smoothly over time so that there isn't an instantaneous jump/jerk when you press C, unless that's what you want.