Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
2
Question by SrBilyon · Jan 28, 2011 at 04:20 PM · camerazoomfocusparty

Keep Player + Party and/or Targeted Enemies in Camera Focus

I was planning on making a multiplayer game that local multiplayer, but decided that I would have the game on one computer.

My camera currently allows 4 four players to be on a screen at once. The game I intend on creating will allow up 2-4 for players and I'm thinking that want to limit the distance they can move based on a zoom out limit variable or make them teleport behind the 1st player if they get to far from group.

The problem is I can't figure out how to zoom in and out when the players get for or closer. I also want it to become a little bit "top-down" as the players get farther away from each other and more third person when the get closer.

This might help you get the idea (even though the game is 2D) I want the camera to zoom in-out based on the player's distance from each other. I would also like it not to just be limited to 4 players, but for 2-3 players also.

Here is the camera script I have so far.

//======================================== //Currently, there are for slots to put players in for camera focus var lookAtTarget1 : Transform; var lookAtTarget2 : Transform; var lookAtTarget3 : Transform; var lookAtTarget4 : Transform;

//Place the camera under this in the inspector var theCamera : Transform;

//How high the camera is currently (should be replaced later for zom variables) var height : float = 10.0;

//How close the camera is to the average spot that it is focused at. var distance = 12.0;

//Temp Zoom Variable var zoom = 10.0;

//This checks the average location of the players in coordinates. var avgDistance; //========================================

function LateUpdate ()

{
//If the first target doesn't exist, exit this statement if (!lookAtTarget1) return;

   //Check for the average distance between the four players.
   avgDistance = ((lookAtTarget1.position+lookAtTarget2.position+lookAtTarget3.position+lookAtTarget4.position)/4);
   Debug.Log(avgDistance);

   theCamera.transform.position.x = avgDistance.x ;
   theCamera.transform.position.z = avgDistance.z - distance;
   theCamera.transform.position.y = avgDistance.y + height;


     //Set height and distance variable to zoom variable
         height = zoom;
     distance = zoom;

}

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 Wiki

Answer by reissgrant · Jan 31, 2011 at 04:28 AM

This script takes the largest difference between all object positions and lerps the camera with that value. The larger the difference between objects, the greater the camera distance.

To test, assign 4 cubes to the script, and move them in the editor after hitting play. The camera will move according to the greatest overall distance.

*EDIT this script will search for any gameobjects with the tag "Player" and assign those to the targets array. This happens every update so if a target dies, it is removed from the target array immediately. Also, it is not limited to 4, you can use as many targets as you like, however only targets with the "Player" tag will be included.

*EDIT 2 upated to fix a small bug. 12:26

*EDIT 3 updated to fix player being clipped. 11:21 -- 1/31

*EDIT 4 updated to allow Orthographic Cameras (it will auto detect Orthographic camera) 10:28 -- 2/26/12

////////////////////////////////////////////////////////////////////

 //IMPORTANT! Tag ALL players with "Player" so they are recognized.//

 ////////////////////////////////////////////////////////////////////  





private var isOrthographic : boolean; var targets : GameObject[]; var currentDistance : float; var largestDistance : float; var theCamera : Camera; var height : float = 5.0; var avgDistance; var distance = 0.0; // Default Distance var speed = 1; var offset : float;

//========================================

function Start(){

 targets = GameObject.FindGameObjectsWithTag("Player"); 

 if(theCamera) isOrthographic = theCamera.orthographic;

}

function OnGUI(){

 GUILayout.Label("largest distance is = " + largestDistance.ToString());

 GUILayout.Label("height = " + height.ToString());

 GUILayout.Label("number of players = " + targets.length.ToString());

}

function LateUpdate ()

{

 targets = GameObject.FindGameObjectsWithTag("Player"); 

 if (!GameObject.FindWithTag("Player"))

 return;

 var sum = Vector3(0,0,0);

 for (var n = 0; n < targets.length ; n++){

     sum += targets[n].transform.position;

 }

   var avgDistance = sum / targets.length;

// Debug.Log(avgDistance);

   var largestDifference = returnLargestDifference();

   height = Mathf.Lerp(height,largestDifference,Time.deltaTime * speed);

     if(isOrthographic){

         theCamera.transform.position.x = avgDistance.x ;

         theCamera.orthographicSize = largestDifference;

         theCamera.transform.position.y = height;

         theCamera.transform.LookAt(avgDistance);

     } else {

         theCamera.transform.position.x = avgDistance.x ;

         theCamera.transform.position.z = avgDistance.z - distance + largestDifference;

         theCamera.transform.position.y = height;

         theCamera.transform.LookAt(avgDistance);

     }

}

function returnLargestDifference(){

 currentDistance = 0.0;

 largestDistance = 0.0;

 for(var i = 0; i < targets.length; i++){

     for(var j = 0; j <  targets.length; j++){

         currentDistance = Vector3.Distance(targets[i].transform.position,targets[j].transform.position);

         if(currentDistance > largestDistance){

             largestDistance = currentDistance;

         }

     }

 }

 return largestDistance;

}

Comment
Add comment · Show 14 · 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 SrBilyon · Jan 31, 2011 at 04:39 AM 0
Share

Nice, if you could help me with the zoom, I could declare this answered!

avatar image reissgrant · Jan 31, 2011 at 04:44 AM 0
Share

Do you mean the third-person to top-down view?

avatar image reissgrant · Jan 31, 2011 at 04:57 AM 1
Share

Ok check this new code I just posted, and I'll add the player number.

avatar image reissgrant · Jan 31, 2011 at 05:32 AM 1
Share

I see what you mean, changing speed doesn't seem to help much. I have to sleep now, but will have time to look at it tomorrow morning. I'll post back a fix then. ;)

avatar image reissgrant · Jan 31, 2011 at 04:23 PM 1
Share

O$$anonymous$$, sorry for the delay, it is fixed now. The largest difference is added to the distance now so the player will not be clipped. Let me know how it works for you.

Show more comments

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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Framing an Object, Bounds & Encapsulating 0 Answers

Losing Focus When Zooming In And Out 1 Answer

Lining up Fov's 0 Answers

Any ideas for doing a digital camera zoom? 1 Answer

Change UI scale in relation to 3D camera 0 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