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 /
avatar image
0
Question by Julian Witte · Aug 11, 2011 at 07:29 PM · cameraorthographichudperspectiveezgui

Orthographic objects follow in perspective

I'm using EZ GUI for HUD and I would like some elements like a health-bar to follow my hero in the scene, but I don't want those HUD elements to have perspective, I don't want them to take a larger amount of the screen as my hero gets closer to the camera. So I did the following:

     Vector3 screenpos = cam.WorldToScreenPoint(new Vector3(myParent.position.x, myParent.position.y+1, myParent.position.z));
     transform.position = GUICam.ScreenToWorldPoint(screenpos);

Where cam is the main-perspective camera and GUICam is an orthographic camera for HUD. That works, the HUD follows my hero as it moves, but it shakes on the screen for every movement, it happens when used on Update() and LateUpdate(). So I tried OnGUI() for a faster update rate and it shows now smooth and perfect.

The point is, as a mobile application, I don't want to use OnGUI() functions on my project to save performance. Is there some way I can optimize that?

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

Answer by equalsequals · Aug 11, 2011 at 09:00 PM

I believe the shaking you are referring to could be the fact that your WorldToScreenPoint is taking a Vector3 and outputting a Vector3, which is made of floats. In a vector environment this would be okay, but in a raster environment you get shakes and slight distortions when it comes to fractional pixels.

It might be beneficial to use LateUpdate to take advantage of the execution order, and ensure that the Vector3 you are using to assign the screenspace position of your health bar is comprised of only floats. You can do this by doing something like this:

 Vector3 screenpos = cam.WorldToScreenPoint(new Vector3(myParent.position.x, myParent.position.y+1, myParent.position.z));
 transform.position = GUICam.ScreenToWorldPoint(new Vector3((int) screenpos.x, (int) screenpos.y, (int) screenpos.z)); //typecasting a float to an int automatically drops anything after the decimal.

Or this is essentially the same thing:

 transform.position = GUICam.ScreenToWorldPoint(new Vector3(Mathf.Round(screenpos.x), Mathf.Round(screenpos.y), Mathf.Round(screenpos.z)));  //You could also substitute Round with Ceil() or Floor() if you wanted fine-tuned control of this.

If that doesn't do it, I have another idea what it could be - I also do not use EZGUI so I am making speculations on some of it's functionality as to how it handles certain things.

Hope that helps.

==

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
0

Answer by Julian Witte · Aug 11, 2011 at 09:32 PM

Hi, equalsequals,

EZ GUI generates 3D meshes to draw interface elements, thats the reason why they are affected by perspective cameras.

In that case, I can't cast those values to int, otherwise that will distort the hero (myParent) position in the world received by the camera. That actually stops the shaking, the hud won't follow my hero smoothly, it jumps to the nearest non-decimal position points in the world.

I just figured out that, the shaking is caused by the camera smooth follow behaviour, it shakes as the camera smoothly adjust its position to follow the hero. I've got a better result using a Vector3.Lerp to update the HUD position, so the shaking is no longer noticeable.

Thanks!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Draw orthographic visuals (e.g. health bar) in perspective scene? 4 Answers

Using a perspective camera can an object act as if viewed by orthographic camera? 1 Answer

Camera 2D RPG in perspective 1 Answer

fluent animation from orthographic to perspective projection 2 Answers

Click and Drag Camera only works in Orthographic camera 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