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 Jesus_Freak · Dec 20, 2010 at 06:50 PM · errormousepositionscreentoworldpoint

ScreenToWorldPoint error

hello, i have a scene with a player prefab that contains a main capsule with various scripts, an orthographic camera that is a child of that capsule, and a (-n obsolete) spawnpoint.

i'm using this bit of code to draw a Gizmo on the screen and calculating where the world coordinates are, and it seems off:

var cam : Camera; var Val : int = 8;

 function OnDrawGizmosSelected()
  {

      var p : Vector3 = cam.ScreenToWorldPoint(Vector3(Input.mousePosition.x,Input.mousePosition.y, Val));
      Gizmos.color = Color.yellow;
      Gizmos.DrawSphere(p,1);



      }
 function Update()

{ print(Input.mousePosition); }

and it looks perfect in the game view, but in the scene view.... it's a little off, and if i were to put a transform.position = ScreenToWorldPoint (my main goal), it would be wrong,

so is there a way to make the screen coords the ACTUAL world coords so that if i put my mouse on an object say at game(world) 0,5,0 in the game mode, then in the scene view, it would move the Gizmo to the scene(world) 0,5,0?

as always, any help is appreciated!

thank you!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by fonql · Jan 28, 2012 at 10:36 AM

I had the same problem. changing the nearClipPlane value to the 'z' position value of the camera times -1! really wierd, but it worked:

 var target : Transform;function Update () {

 var screenPos : Vector3 = Camera.main.WorldToScreenPoint (target.position);
 print ("target is " + screenPos.x + " pixels from the left");
 print ("target is " + screenPos.y + " pixels from the bottom");
 var worldPos : Vector3 = Camera.main.ScreenToWorldPoint(Vector3(screenPos.x,screenPos.y, (Camera.main.transform.position.z*-1)));
 print(worldPos);

}

Only thing is that z will always be 0.

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 Tasarran · Jan 28, 2012 at 03:17 PM

The thing is, if you define a point on the screen, in 3D, it defines a line that goes from the camera, through that screen point, and runs out to infinity.

As you're finding, it doesn't tell you where to stop. ('depth' coordinate)

I haven't had any experience with this yet, but what I have read about people doing is using a RayCast from the camera, through the Screen Point, and read the point where it hits something in your scene as the point you want.

Does this make any sense?

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 fonql · Jan 29, 2012 at 10:05 AM

yes, that definetely makes sense :) thanks for your answer.

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

1 Person is following this question.

avatar image

Related Questions

Having player object always snap to mouse position 1 Answer

Need explanation for this code 0 Answers

ScreenToWorldPoint question 1 Answer

ScreenToWorldCoordinates not accurate 1 Answer

Mouse position for player movement 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