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 /
avatar image
0
Question by Robs · Mar 27, 2014 at 05:52 AM · camera2dorthographic

Keeping two objects in frame

I am working on a 2D endless runner and I am having a hard time trying to figure out how to "zoom" the camera in and out based on the height of the player from the ground while keeping both the player and the ground in view. I am trying to change the size of my orthographic camera, but just doing that alone doesn't get the effect that I am trying to accomplish. Any help would be greatly appreciated.

Here is the effect that I am going for: http://youtu.be/uHlulsVgHzc?t=2m14s

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 robertbu · Mar 27, 2014 at 07:08 AM

Here is a bit of code to get you started. I'm assuming an orthographic camera. Variables explained:

  • target - object to follow

  • bottom - world 'y' position that will be right at the bottom of the camera no matter what the ortiographicSize is set to.

  • lead - world units on how much to lead the target. You could add code to vary this value based on the orthographic size.

  • above - amount of space to leave above the pivot of the target

  • minView - the lower limit to the world units seen by the camera. That is, if the distance between the bottom and the target plus the above is less than this amount, the camera will no longer zoom. I added this based on what I saw in the video.

This script should be added to the camera:

 #pragma strict
 
 var target : Transform;
 var bottom : float = -4.0;  // Bottom edge seen by the camera
 var lead : float = 2.5; // amout to lead the target
 var above : float = 1.5; // amount above the target pivot to see
 var minView : float = 7.5;
 
 function LateUpdate() {
     var height = target.position.y + above - bottom;
     height = Mathf.Max(height, minView);
     height /= 2.0;
     camera.orthographicSize = height;
     transform.position = Vector3(target.position.x + lead, bottom + height, -10); 
 }
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 Robs · Mar 27, 2014 at 05:48 PM 0
Share

This worked! Thank you very much!

avatar image neilsonbird · Jul 15, 2015 at 06:18 PM 0
Share

Hi there, I'm working on something similar and this is exactly the effect I'm looking for, although I'm trying to get this to work not just on the y axis from the ground, but from all sides. If my object was to slide left, right, up, or down I'd like the camera to zoom out to keep it in frame.

I've tried editing this with no luck on getting it to work, unfortunately. Could you explain how I could achieve this effect?

avatar image pr0ph3ts · Jan 12, 2018 at 08:52 PM 0
Share

Honestly, thank you, I used this idea converted to a c# code for a problem that had been annoying me for weeks!!! Another pla$$anonymous$$u reached!

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

21 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

Related Questions

Making a camera semi-orthographic 2 Answers

Orthographic camera movement clamping 1 Answer

parallax scrolling using orthographic camera 1 Answer

Dynamic Orthagraphic Camera Zoom 1 Answer

Resizing orthographic camera to fit 2d sprite on screen 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