Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by Jimmythesaint82 · Feb 15, 2013 at 02:40 PM · camerascreenzoomoffset

Camera zoom while keeping an off-center object at the same screen co-ordinates

Camera zoom whiloe keeping an offcenter object at the same screen co-ordinates.

Im doing a zoom by dragging on the highligted red ball below.

How do I get the ball to stay at it's screen coords while the rest of the view zooms in?

Thanks in advance

James

alt text

alt text

tabula.jpg (390.8 kB)
tabula2.jpg (327.3 kB)
Comment
Add comment · Show 4
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 robertbu · Feb 15, 2013 at 05:55 PM 0
Share

Are you using an Orthographic camera?

avatar image Jimmythesaint82 · Feb 15, 2013 at 06:22 PM 0
Share

Nope a perspective camera as there is some 3d effects going on too...

avatar image robertbu · Feb 15, 2013 at 06:44 PM 0
Share

Are you zoo$$anonymous$$g the camera by changing the field of view or by moving the camera towards the subject?

avatar image Jimmythesaint82 · Feb 15, 2013 at 07:19 PM 0
Share

Currently by moving the camera... though not adverse to using FOV

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · Feb 15, 2013 at 07:54 PM

I've not got time right now to write the test code, but here is the idea:

  • Use Camera.main.WorldToViewportPoint() before making the move (or even in Start()) to get the position in viewport space of the object.

  • Move the camera in for the zoom.

  • Get the new Viewport coordinates for the new position of the object.

  • Using the new distance between the camera and the board, calculate the world positions for the two viewport positions using Camera.ViewportToWorldPoint();

  • Subtract the two to get how far to translate the board or the camera.

Comment
Add comment · Show 7 · 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 robertbu · Feb 15, 2013 at 08:57 PM 1
Share

Got a chance to test it in code:

 public class ZoomWithOffCenterObject : $$anonymous$$onoBehaviour {
     
     public GameObject goTrack;
     private Vector3 v3ZoomFactor = new Vector3(0.0f, 0.0f, 0.05f);
 
     void Update () {
         if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.UpArrow))
             ZoomCamera (true);
         if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.DownArrow))
             ZoomCamera (false);
     }
     
     private void ZoomCamera(bool bIn) {
         Vector3 v3OrgPos = Camera.main.WorldToViewportPoint(goTrack.transform.position);
         
         if (bIn)
             transform.Translate (v3ZoomFactor);
         else
             transform.Translate (-v3ZoomFactor);
         
         v3OrgPos.z = $$anonymous$$athf.Abs (Camera.main.transform.position.z - goTrack.transform.position.z);
         v3OrgPos = Camera.main.ViewportToWorldPoint(v3OrgPos);
         transform.Translate (goTrack.transform.position - v3OrgPos);
     }
 }
avatar image Jimmythesaint82 · Feb 16, 2013 at 03:33 PM 0
Share

Looks really good, and works in a clean scene...

However my camera tracks in Y and I can't for the life of me get this to handle that...

Any ideas?

avatar image Jimmythesaint82 · Feb 16, 2013 at 03:34 PM 0
Share

As in y is up

avatar image robertbu · Feb 16, 2013 at 05:07 PM 0
Share

The code above looks down the Z axis, so Y is up. Are you saying your camera is looking down the Y axis at the XZ plane?

avatar image Jimmythesaint82 · Feb 17, 2013 at 01:26 AM 0
Share

yes, thats what I meant :)

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

9 People are following this question.

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

Related Questions

Make camera Zoom in or Zoom Out to focus all target objects 0 Answers

How to do isometric camera 1 Answer

How to accurately zoom (not dolly) and pan Camera to mouse click position 1 Answer

Lock the screen 1 Answer

Camera Shake 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