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 TheQuantumGamer · Feb 05, 2015 at 11:18 AM · gameobjectpositionmousepositionscreentoworldpoint

ScreenToWorldCoordinates not accurate

What I'm trying to do is Instantiate an object at a touch position. Here is my code:

         Vector3 sp = Input.mousePosition;
         Vector3 wp = Camera.main.ScreenToWorldPoint(new Vector3(sp.x,sp.y,10));
         GameObject g = Instantiate(Resources.Load("Player"), wp,Quaternion.identity) as GameObject;

However is seems my wp variable is not accurate because the object spawns at the wrong location (around 15 unit left and 3 units down). I'v checked that it is not the objects fault that it is changing location, so what could it be? Any help would be appreciated.

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 AlwaysSunny · Feb 04, 2015 at 09:45 PM 0
Share

Throw in a Debug.DrawRay(Vector3.zero, wp); and see if it points at the right spot.

avatar image TheQuantumGamer · Feb 05, 2015 at 03:53 PM 0
Share

Ok so I just tried it but the ray doesn't appear in my scene window or game window

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by daneislazy · Feb 05, 2015 at 04:51 PM

Yep, sounds about right. I was using ScreenToWorld a while ago and was having similar issues. What is probably happening is that the Input.mousePosition and ScreenToWorld are not using the same coordinate rules(even though the documentation says that they do), that was my issue but I was using Event.current.mousePosition instead. Also only my vertical/y axis was messed up. I fixed it by making a function specifically to fix the vector and using ScreenToWorldPoint(new Vector3(vIn.x, Screen.height - vIn.y)).

I'm not sure if that will work in this case though since you seem to be having horizontal and vertical issues. Try putting in a Debug message of what the sp values are after you get them. According to the docs the lower left of the screen should be 0,0 and top right should be pixelHeight, pixelWidth.

Edit: ok just checked in my game and Input.mousePosition is working correctly. So that might not be the cause. Urm... make sure that the camera you are wanting to use is set to main camera and not something else. It might also be the depth value as that might not translate to the right point with a perspective camera, possibly aggravated if the camera is at an angle like isomentric or something... just spit balling because my initial idea was probably wrong.

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 TheQuantumGamer · Feb 06, 2015 at 04:45 PM 0
Share

I checked my coordinates and the lower left is 0,0 I also tried ScreenToWorldPoint(new Vector3(vIn.x, Screen.height - vIn.y)) but it did not work

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

[CODE]Keep Gameobject position exactly at mouse cursor when cast float position to int 1 Answer

Different Position (Object position - Mouse Position) 1 Answer

How to keep the same position of the object instead of camera 0 Answers

ScreenToWorldPoint and Mouse Position 2 Answers

Update character's position after animation 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