Search Unity

Move Camera to Moving Object and Stay With Object

Discussion in '2D' started by Shmoji, Feb 23, 2019.

  1. Shmoji

    Shmoji

    Joined:
    Nov 16, 2017
    Posts:
    14
    Basically, I have little squares falling from the sky. I want to be able to click on a square and then the center of the camera goes to the center of the object (smoothly) and continues to follow it.

    I have got pretty close, but every time there is some little glitch that I cannot get past. My first thought was to use SmoothDamp to get to the location of the object and then once about there, set the camera's parent as the object.

    I think the hard part is the fact that I am trying to move to a moving target. Does anyone have any ideas how this could be implemented??
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @Shmoji

    Doesn't sound like it couldn't work.

    But you could also try Vector3.MoveTowards instead of SmoothDamp.

    Other than that, show your code if you think you have some particular issue with your approach.

    Otherwise everyone will be just guessing, because you told very little, other than that there is some little glitch that you cannot get past.
     
  3. Shmoji

    Shmoji

    Joined:
    Nov 16, 2017
    Posts:
    14
    I tried figuring it out for days, then, of course, right after I posted about it, I figured it out. If anyone else ends up running into this same issue, just reply on this thread and I will post my code. However, I don't know if it would help because it is very specific to my project.

    I would like to add: if you ever get stuck for a long period of time, I recommend you do tons of research, but also trust your own ideas and code. I got stuck because I was trying to figure out how to make code taken from the internet work in my project. As soon as I erased all borrowed code and restarted using my own ideas, I figured my issue out.