• 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
1
Question by Kalbytron · Feb 20, 2014 at 07:37 PM · camerazoomorthographic

How to make a smash bros-like camera

I have been making a multiplayer game with the camera focusing on everyone on the screen. Just like smash bros, the camera focuses on the center of all of the characters positions w$$anonymous$$le zooming to keep the players viewable.

My only problem is the zooming with an orthograp$$anonymous$$c camera. So, is there anyone who knows how to zoom the camera to keep the players in view?
(Sample Code would be nice)

Comment
Add comment · Show 17
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 Kalbytron · Feb 20, 2014 at 08:54 PM 0
Share

Alright, but how do I make the camera know how much to change the size of the viewport to keep every player on screen?

avatar image CodeElemental · Feb 20, 2014 at 09:05 PM 1
Share

Well , after the rect calculation, the center of the rect can be calculated ((leftx + rightx) /2 , (topy + boty) / 2). You set the camera to look at that point.

The size of the camera is formed by getting the "greater" dimension (either width or height) , and the other one will be calculated preserving the camera ratio.

ex : camera ratio is 4:3 , rect width is 700 , rect height is 300 the final rect will be half of the vertical volume, but the vertical volume has to preserve the ratio , hence 700 * 3/4 = 525 the orthographic size is half the vertical dimension = 212.5

avatar image Kalbytron · Feb 20, 2014 at 09:14 PM 0
Share

Hmm that is interesting. You also mentioned that I can put a Rect around the object that it is displaying. How would you do that, and for multiple players?

avatar image CodeElemental · Feb 20, 2014 at 09:26 PM 3
Share

I used the rect to reference the viewport rectangle. As ilustrated on this picture alt text

camera.png (5.6 kB)
avatar image AlucardJay · Feb 25, 2014 at 08:27 AM 2
Share

CodinRonin has given you lots of information to get started on this. From the first comment :

you can iterate through the positions of all the players and get the Rect in which they are all displayed (the topmost and lowest y , the leftmost and rightmost x )

Have you done this yet?

PseudoCode :

 var minX : float = Mathf.infinity;
 var maxX : float = -Mathf.infinity;
 var minY : float = Mathf.infinity;
 var maxY : float = -Mathf.infinity;
 
 for each player
 {
     if player.x < minX
         minX = player.x 
     else if player.x > maxX
         maxX = player.x 
     
     if player.y < minY
         minY = player.y 
     else if player.y > maxY
         maxY = player.y 
 }

from this you will have the bottom-left and top-right corner of your bounding rect area. You should also add a buffer so the player is not cut in half on the edge of the screen.

Edit the question with the progress and code you have made so far.

Show more comments

3 Replies

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

Answer by Kalbytron · Mar 17, 2014 at 08:23 PM

Sorry about me taking a w$$anonymous$$le to say thanks for the help. I'm just going to post t$$anonymous$$s for everyone to view.

Thanks to CodinRonin and alucardj.

// Update is called once per frame void Update() {

CalculateBounds();

CalculateCameraPosAndSize();

}

void CalculateBounds() { minX = Mathf.Infinity; maxX = -Mathf.Infinity; minY = Mathf.Infinity; maxY = -Mathf.Infinity;

 players = GameObject.FindGameObjectsWithTag("Player");

 foreach (GameObject player in players){

    Vector3 tempPlayer = player.transform.position;

    //X Bounds
    if (tempPlayer.x < minX)
      minX = tempPlayer.x;

    if (tempPlayer.x > maxX)
      maxX = tempPlayer.x;

    //Y Bounds
    if (tempPlayer.y < minY)
      minY = tempPlayer.y;

    if (tempPlayer.y > maxY)
      maxY = tempPlayer.y;
 }

}

void CalculateCameraPosAndSize() { //Position Vector3 cameraCenter = Vector3.zero;

 foreach(GameObject player in players){
    cameraCenter += player.transform.position;
 }

 Vector3 finalCameraCenter = cameraCenter / players.Length;

 //Rotates and Positions camera around a point
 rot = Quaternion.Euler(angles);
 pos = rot * new Vector3(0f, 0f, -camDist) + finalCameraCenter; 

 transform.rotation = rot;
 transform.position = Vector3.Lerp(transform.position, pos, camSpeed * Time.deltaTime);

 finalLookAt = Vector3.Lerp (finalLookAt, finalCameraCenter, camSpeed * Time.deltaTime);

 transform.LookAt(finalLookAt);

 //Size
 float sizeX = maxX - minX + cameraBuffer.x;
 float sizeY = maxY - minY + cameraBuffer.y;

 camSize = (sizeX > sizeY ? sizeX : sizeY);

 camera.orthograp$$anonymous$$cSize = camSize * 0.5f;

}

Comment
Add comment · Show 3 · 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 GaborV · Mar 08, 2016 at 12:26 AM 0
Share

I know this is a little bit old, but now I want to have this kind of camera in my game. I tried this code but it's not okay: Camera movement How to solve this? Did you found a good solution?

avatar image hongdeyuteo · Jan 01, 2021 at 04:42 PM 0
Share

i made a similar camera system until i tried removing and adding the targets into the list when the players died. Adding the targets back to the array were very challenging and i could not find a way to do so as List.Add() does not work if the element is 0. i tried List.insert but it still didn;t work. it's like 4 am now and im gonna go nuts soon pls help :(((( thanks in advance!

avatar image Spip5 hongdeyuteo · Jan 02, 2021 at 02:38 PM 0
Share

Have a look at this, this is from brackeys channel (they used to do really nice tuorials) : https://www.youtube.com/watch?v=aLpixrPvlB8

avatar image
1

Answer by CodeElemental · Feb 20, 2014 at 08:14 PM

You can zoom the orthograp$$anonymous$$c camera by adjusting it's orthograp$$anonymous$$c size . As for the positions , you can iterate through the positions of all the players and get the Rect in w$$anonymous$$ch they are all displayed (the topmost and lowest y , the leftmost and rightmost x ) , and center the camera to look at that rect.

Comment
Add comment · 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
2

Answer by angelsin · Dec 22, 2016 at 11:46 AM

@GaborV unity 5 answer using a perspective cam

 using System.Collections.Generic;
 using UnityEngine;
 using System.Linq;
 
 namespace Assets.Scripts.Player
 {
     public class PlayerCam : MonoBehaviour
     {
         private new Transform transform;
         private Vector3 DesiredPos;
         public List<Transform> Players;
         public float camSpeed;
         private Camera cam;
         void Awake()
         {
             transform = GetComponent<Transform>();
             cam = GetComponent<Camera>();            
         }
 
         private void Start()
         {
             var p = GameObject.FindGameObjectsWithTag("Player");
             Players = new List<Transform>();
             for (int i = 0; i < p.Length; i++)
             {
                 Players.Add(p[i].GetComponent<Transform>());
             }
         }
 
         void Update()
         {
             if (Players.Count <= 0)//early out if no players have been found
                 return;
             DesiredPos = Vector3.zero;
             float distance = 0f;
             var hSort = Players.OrderByDescending(p => p.position.y);
             var wSort = Players.OrderByDescending(p => p.position.x);
             var mHeight = hSort.First().position.y - hSort.Last().position.y;
             var mWidth = wSort.First().position.x - wSort.Last().position.x;
             var distanceH = -(mHeight + 5f) * 0.5f / Mathf.Tan(cam.fieldOfView * 0.5f * Mathf.Deg2Rad);
             var distanceW = -(mWidth / cam.aspect + 5f) * 0.5f / Mathf.Tan(cam.fieldOfView * 0.5f * Mathf.Deg2Rad);
             distance = distanceH < distanceW ? distanceH : distanceW;
 
             for (int i = 0; i < Players.Count; i++)
             {
                 DesiredPos += Players[i].position;
             }
             if (distance > -10f) distance = -10f;
             DesiredPos /= Players.Count;
             DesiredPos.z = distance;
         }
 
         void LateUpdate()
         {
             transform.position = Vector3.MoveTowards(transform.position, DesiredPos, camSpeed);
         }
     }
 }
 
Comment
Add comment · 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

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

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

zoom in with orthografic camera with focus on the a specific point 0 Answers

2D camera zoom in comparison to the height of target 2 Answers

How do you zoom with an orthographic camera? 1 Answer

Implementing Camera bounds proportionally to its ortographic size 0 Answers

Ortho camera zoom to mouse point 4 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