• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

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

Rendering a 3D object within a specific area/bound? 1 Answer

orthographic camera view looks different from perspective view 2 Answers

Drawing on the same screen with different cameras? 1 Answer

Orthographic position to Perspective position 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges