• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DanjelRicci · Sep 16, 2010 at 08:37 PM · cameraplayerfollowsmooth

How to make a camera that follows more than one character?

I have some Jelly characters moving togheter on a grid, like miniatures on a game board. Since the board is very large and every Jelly is an human player, they must be all visible on the screen. So, I need the camera to zoom in and out, and follow smoothly the Jellies to watch the entre area covered by the players.

How can I do that? I am really clueless on this.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by equalsequals · Sep 16, 2010 at 08:45 PM

To get you started, you should be able to use some straight forward Vector3 calculations to find a center point between all of the players' positions.

From there you should make that Vector3 the target of your camera so it follows/centers on it. To tackle the zoom, since you should know all the players positions already, you could find the 2 who are farthest apart from each other and use that when calculating where your camera should be on the Y axis.

[EDIT]

In response to your comment...

You could use the Mesh class to create a mesh that uses all of the player's transform.position Vector3's as it's vertices.

From there, you can use the Mesh.bounds property to return an Axis Aligned Bounding Box (AABB) which you can use the Bounds.center property (a Vector3) as the focal point of your camera. Then, because you have an AABB, the larger the number, the further out your camera should be, and vice-versa.

Here's a little something to get you started:

using UnityEngine; using System.Collections;

public class FramePlayers : MonoBehaviour {

 //set this in the inspector
 public Transform[] playerLocs; 

 private Mesh m;

 void Update () {

     //need a new mesh every frame
     m = new Mesh();

     //new set of vector3s aswell
     Vector3[] v3s = new Vector3[playerLocs.Length];

     //populate
     for(int i = 0; i < playerLocs.Length; i++)
     {
         v3s[i] = playerLocs[i].position;
     }

     //asign them to the mesh
     m.vertices = v3s;

     //print the resulting AABB's center. I tested this with an even and odd number of transforms.
     print(m.bounds.center + "is the center of this mesh.");

 }

}

Hope that helps.

==

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DanjelRicci · Sep 17, 2010 at 01:11 PM 0
Share

This is exactly what I already thought to do, but the problems is: what are these Vector3 calculations?

I thought to build an imaginary polygon where players are the vertices. The polygon's barycentre will obviously be the point where camera looks. Then, I should cycle between all players to find who are the farthest, and consequently change the zoom according to the distance.

Unfortunately, the only thing I found to achieve all of this is a piece of code that calculates the distance between 2 objects. At this point, I really don't know where to start...

avatar image equalsequals · Sep 17, 2010 at 01:50 PM 0
Share

Updated answer...

avatar image DanjelRicci · Sep 19, 2010 at 06:54 PM 0
Share

Thanks for the code help, I'll try it as soon as possible!

avatar image runonthespot · Oct 21, 2010 at 03:59 PM 0
Share

This is excellent.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Camera follow smoothness problem 1 Answer

Camera not rotate when following player 1 Answer

Why does the camera smoothly follow in one direction but not the other? 0 Answers

How to get camera to follow player 2d 11 Answers

Smoothly following a moving object 5 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges