Multiple target Camera – Change the object dynamicly.

Updated on January 26, 2018 in [A] Tutorials
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
4 on January 25, 2018

Hello there, the tutorial: https://www.youtube.com/watch?v=aLpixrPvlB8&t=667s

in the tutorial, Brackeys made it so you drag the object in the editor over to the targets and define the object that way.

How can i make it so i choose and change the objects the camera should follow in the script? I want to be able to change the objects that it needs to follow dynamically.

    void Move()
    {
        Vector3 centerPoint = GetCenterPoint();
        Vector3 newPosition = centerPoint + offset;
        transform.position = Vector3.SmoothDamp(transform.position, newPosition, ref velocity, smoothTime);
    }
    float GetGreatestDistance()
    {
        var bounds = new Bounds(targets[0].position, Vector3.zero);
        for (int i = 0; i < targets.Count; i++)
        {
            bounds.Encapsulate(targets[i].position);
        }
        //Returns the with of the box.
        return bounds.size.x;
    }
    Vector3 GetCenterPoint()
    {
        if(targets.Count == 1)
        {
            return targets[0].position;
        }
        var bounds = new Bounds(targets[0].position, Vector3.zero);
        for (int i = 0; i < targets.Count; i++)
        {
            bounds.Encapsulate(targets[i].position);
       }
        return bounds.center;
    }
  • Liked by
Reply
3 on January 25, 2018

All targets are saved in the targets variable. So change the objects in there 😉

 

on January 26, 2018

Can you quickly give me an example on how i would manually add a object to targets? 🙂 And maybe how i can update the existence objects?

on January 26, 2018

nvm, found a solution 🙂

on January 26, 2018

Cool!

Show more replies
  • Liked by
Reply
Cancel