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 Anthonylinx · Dec 03, 2013 at 04:20 AM · screenpostion

Drawing Objects Using Window Coords

Could someone explain to me how I would go about drawing a sprite at a location in the window. For example, if I wanted to draw it at the bottom left corner (or 0,0) how would I be able to this?

To be a bit more specific here is what I have so far:

 Vector3 screenPoint = new Vector3(100,0,0);
 Vector3 worldPos = Camera.main.ScreenToWorldPoint( screenPoint );
 Quaternion ZeroRotation = new Quaternion(0,0,0,0);
 
 GameObject tempTile = (GameObject)Instantiate(Resources.Load("Tile"),worldPos,ZeroRotation);

However, rather than drawing the tile at 100,0,0 in my 1920x1080 screen it draws it closer to 350,0,0.

Comment
Add comment · Show 2
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 Spinnernicholas · Dec 03, 2013 at 06:51 AM 0
Share

After running this script in the Editor, what are the coordinates of the:

  • Sprite

  • Recursive Parents of the Sprite

And, have it debug all of the calculations in your script and post them.

This will help us pinpoint what is going wrong.

avatar image Spinnernicholas · Dec 03, 2013 at 06:55 AM 0
Share

Also, try instantiating the sprite and then positioning it.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ThatsAMorais · Dec 03, 2013 at 06:18 AM

You're looking for "Screen", I think. In other words...

 Vector3 screenPoint = new Vector3(Screen.width,Screen.height,0);
 Quaternion ZeroRotation = new Quaternion(0,0,0,0);
  
 GameObject tempTile = (GameObject)Instantiate(Resources.Load("Tile"),screenPoint,ZeroRotation);

This code will place the sprite at the bottom-left.

Comment
Add comment · Show 1 · 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 Anthonylinx · Dec 03, 2013 at 06:21 AM 0
Share

What this ends up doing is making a tile very far to the right way off the screen. In the code snippet I added I'm using ScreenToWorldPoint() which seems to be what I need but it's not giving me my expected results.

avatar image
0

Answer by robertbu · Dec 03, 2013 at 06:24 AM

Your problem is the 'z' parameter you set for your initial screenPoint. This value needs to be set to distance in front of the camera. A value of '0' will place the object at the camera position regardless of the 'x' and 'y' values. So to set it 10 units in front of the camera:

 Vector3 screenPoint = new Vector3(100,0,10);
 Vector3 worldPos = Camera.main.ScreenToWorldPoint( screenPoint );

Note there is a shortcut for zero rotation called 'Quaternion.identity', so you don't need to do your 'zeroRotation' code above just do:

 GameObject tempTile = Instantiate(Resources.Load("Tile"), worldPos, Quaternion.identity) as GameObject;
Comment
Add comment · Show 2 · 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 Anthonylinx · Dec 03, 2013 at 06:26 AM 0
Share

$$anonymous$$y camera is placed at -10 so a value of 0 in the z axis for my object should be fine.

avatar image robertbu · Dec 03, 2013 at 07:03 AM 0
Share

The 'z' parameter is not the position in world space, but the distance in front of the camera. So if your camera is at -10, and you want to deter$$anonymous$$e a screen position at 'z' = 0, you need to enter '10' (i.e. 0 -(-10)) for the 'z' parameter. If your camera was at -20 and the position you were looking for was +10, then you would enter '30' for the 'z' parameter.

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

18 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

Related Questions

GUI texture to fit the screen at any resolution? 4 Answers

Blur only the top and bottom of the screen 1 Answer

Why doesn't my players camera work and why does it spaz out randomly 1 Answer

Can i add another splash screen after the Unity one? 1 Answer

How to make my game fit into my droid? 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