• 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
Question by Diekeke · Jun 26, 2013 at 11:38 PM · cameraguigameobjecttransformscreen

Keep GameObject on a corner of the screen

 var guiCam : GameObject;
 var targetPos : Vector3;
 
 function Update () 
 {
     transform.localPosition = guiCam.camera.ScreenToViewportPoint(targetPos);
 } 

Hi, well, the t$$anonymous$$ng is that I have t$$anonymous$$s script attached to the gameobject that I want to keep on the corner independently of the screen resolution, and the gameobject is a c$$anonymous$$ld of my GUI camera (w$$anonymous$$ch renders all of my gui stuff), but I can't get it to work, I adjust the desire position and everyt$$anonymous$$ng but when I go fullscreen it goes out of the screen, so it's basically not working. I don't know what I'm doing wrong. Please help! Thanks in advance.

Comment
Sildaekar
Shirane85
gtramnull

People who like this

3 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 Eric5h5 · Jun 27, 2013 at 01:06 AM 0
Share

Why are you using ScreenToViewportPoint? GameObjects don't use viewport space.

avatar image Diekeke · Jun 27, 2013 at 01:11 AM 0
Share

Because I saw it in another answer and I really don't know ANYTHING about all those camera functions, plus I already tried with the others but I don't quite know how to used them. I would really apreciate the right way to do it or maybe an example. Thanks.

avatar image robertbu · Jun 27, 2013 at 01:44 AM 0
Share

What kind of a game object is this script attached to? A plane? A GUITexture? Some other game object? Are you trying to get the object exactly in a corner or at an edge, or can you live with approximately in the corner/edge?

avatar image Diekeke · Jun 27, 2013 at 01:55 AM 0
Share

It's a 3D object from blender, it's the hunger meter, I need it to be right behind the life meter which is a heart, and the hunger meter is a circular object surrounding it, the life meter is a GUITexture so it's always at the corner where it is supposed to be, but the hunger meter is not so it changes position with different resolutions

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by robertbu · Jun 27, 2013 at 02:10 AM

You have an issue that is solvable by using blender or by using an empty game object. That is, you need the pivot/anchor/origin of the hunger meter to be in the corner you want to anchor. You can do t$$anonymous$$s by moving the pivot to the corner in blender, or by placing an empty game object in the corner and making the hunger meter a c$$anonymous$$ld of the empty game object. Then you can use Camera.ViewportToWorldPoint() to position the hunger meter. Note you will need to specify a distance in front of the camera to position the object.

 var v3Pos = Vector3(0.0, 1.0, 0.25);
 transform.position = gui.camera.ViewportToWorldPoint(v3Pos);

The above lines will place the object in the upper left corner of the screen 0.25 units in front of the camera.

Comment
Sildaekar
Xitech_
D1sp0s3r
mkusan
Maxi1212
gtramnull
xploreygames

People who like this

7 Show 11 · 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 Diekeke · Jun 27, 2013 at 02:19 AM 0
Share

Wow! I'll definately try that! I'll let you know by tomorrow :)

avatar image Diekeke · Jun 27, 2013 at 08:48 PM 0
Share

Oh my god it totally works! Thank you so much!

avatar image Diekeke · Jun 27, 2013 at 09:11 PM 0
Share

And if I wanted to do the same thing but with the health meter (GUITexture) so there wouldn't be any other problem?

avatar image robertbu · Jun 27, 2013 at 11:55 PM 0
Share

Actually GUITextues use Viewport coordinates already, so you can set the position directly and expect it to change for different screen resolutions/ratios.

avatar image Diekeke · Jun 28, 2013 at 04:51 PM 0
Share

Yeah, but is not 100% accurate, in some resolutions everything's okay, but in others, the H.M. is a little bit to the right according to the L.M., and in others, the L.M. is offset as well, so it doesn't looks so good because they have to be perfectly aligned. Is this something that can be fixed, or do I just have to live with it?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

GUI Window on GameObject location? 1 Answer

Is it possible to link a UI element to a gameobject? 1 Answer

Camera rotation the same as player rotation 1 Answer

Detect whenever the player is near the edges of the screen 2 Answers

Rotating gameobject after mouselook 0 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