Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by RageKit · Feb 26, 2016 at 06:11 PM · cameracamera-movementcamera viewport

Keep 2 players on screen by moving camera back

Hello !

I have a game where i have 2 players moving on a plane.

I want to keep them inside the camera view all the time so I need to back the camera when the move far away from each other.

Right now i'm doing it arbitrarily, by linking the camera distance with player distance proportionally but i'd like to find a way to do it "perfectly".

Here is a quick gif of how it looks like now :

If anyone has an idea.

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

2 Replies

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

Answer by Magius96 · Feb 27, 2016 at 06:39 AM

Let's think back to our lessons from Geometry and Trig, eh?

Ok, so let's visualize that we have two identical right triangles, TriangleA and TriangleB. The Hypotenuse of triangle a runs from player 1 to the camera, and the hypotenuse of triangleb runs from player 2 to the camera.

We also know from those lessons ages ago that if we count a single point on the triangle as our alpha point, that the triangle then has a opposite and an adjacent line. We can deduce that the length of the opposite line will match the distance from either player to the mid point between the players.

So far, we only know one angle, and one line, right? Wrong, we actually already know a second angle. The camera can only see to a specific angle of view. Let's say that the camera can see a full 90 degree angle. Then we know that the angle between the opposite line and the hypotenuse is half of 90 degreees.

Now, we have enough information to work with using what we know of SohCahToa.

Sine = Opposite / Hypotenuse Cosine = adjacent / hypotenuse Tangent = opposite / adjacent

What's that you say? Oh yeah, we only know one of those lengths. But we know two angles, using those two angles we can determine the third angle, because all the angles in a triangle add up to 180.

so we can use one of those angles to figure out the length of the adjacent line, which would be the distance the camera would need to be from the scene for both players to still be on camera.

adjacent = tangent * opposite

That's an inversion of the tangent formula above, we just multiplied both sides by opposite. so lets say that the players are 100 points away from each other, so the mid point is 50 points. And as we mentioned before our camera can see 90 degrees, so the angle of the hypotenuse from the opposite is about 45 degrees then our formula should look like this:

adjacent = Mathf.Tan(45) * 50;

Theoretically that adjacent value is the distance from the scene that the camera will need to be. However at this point I do need to point out that you will have to do some radian to degree transformations in all this mess. I don't remember off the top of my head at what point or in which direction it needs to be. I'm feeling like you should multiply the resulting adjacent value by Mathf.Rad2Deg to get the real results, but as I'm not actively working this out in Unity, I very well could be wrong.

Comment
Add comment · Show 2 · 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 Magius96 · Feb 27, 2016 at 06:42 AM 0
Share

While my answer is on the right track, I can guarantee that it's not spot on, but I hope this will provide you enough lead to find the correct result yourself.

avatar image RageKit · Jul 13, 2016 at 10:40 PM 0
Share

Thanks for the answer. I didn't see it (it's been quite some time). I put the project on hold but I'll try to implement your solution as soon as I'm working on it again

avatar image
0

Answer by jktaylo1 · Aug 20, 2017 at 09:52 AM

Magius is right with the trigonometry, but it's easier than this because tan(45) is 1, so just move the camera back away in the z direction in the value of half the distance between your players.

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Create Camera Bounds. In the camera with a different Viewport rect 0 Answers

Try To Set Camera Bounderis 3D 0 Answers

Camera bounds with zoomable camera 1 Answer

Clamping when Zoom changes? 0 Answers

Switching Cameras, Movement not working. 1 Answer


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