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 daum · Nov 08, 2011 at 10:44 AM · guitexturemousepositionscreentoworldpointcamera.main

camera.main update problems with screenToWorldPoint

Hi There,

I am a bit stuck with the following problem:

I am trying to get a line renderer working by creating a world object from mouse/touch screen coordinates (screenToWorldPoint). For debugging I use a GUITexture, displaying the coordinates. Problem is, that when I use camera.current, the counter updates fine only in scene view (not game view window) (but the usual problems with the camera.current being sometimes null occur) -> so naturally i use camera.main, but then the counter stops updating completely (i only get a single first value)... no idea what i am doing wrong here (using os x lion for testing)

script for the mouse coordinate to world coordinate conversion and counter display

 var text : GUIText;
 
 function Update () {
     var mpos : Vector3 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     text.text = "X:"+mpos.x+" - Y:"+mpos.y;
 } 


[Edit:] Simplification Script attached to an empty GameObject in the scene:

 function Update() {
         Debug.Log("world:"+Camera.main.ScreenToWorldPoint(Input.mousePosition)); //no updates
         Debug.Log("screen:"+Input.mousePosition); //updates fine
 }
Comment
Add comment · Show 5
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 syclamoth · Nov 08, 2011 at 10:52 AM 1
Share

Do you have a camera with the tag '$$anonymous$$ain Camera'? Camera.main doesn't do anything magical, it just returns the first camera in the scene with that tag.

In the scene view, I thing Camera.current can return the (invisible) camera which is used to render the scene viewer! (maybe).

avatar image daum · Nov 08, 2011 at 11:07 AM 0
Share

Yes, got one camera, tagged '$$anonymous$$ain Camera'.

$$anonymous$$aybe I don't understand the Update() method correctly? It should automatically be executed every single frame, right?

avatar image syclamoth · Nov 08, 2011 at 11:16 AM 0
Share

Yes, but not while the game is paused (which entering the scene view automatically does), or inside the editor when the game is not running.

avatar image daum · Nov 08, 2011 at 11:21 AM 0
Share

When I simplify my test and just watch the debug output, then the following gets updated every frame Debug.Log(Input.mousePosition); but the conversion to world coordinates only once on start (which is where my problem comes from) Debug.Log(Camera.main.ScreenToWorldPoint(Input.mousePosition));

avatar image syclamoth · Nov 08, 2011 at 11:36 AM 0
Share

That's odd.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by daum · Nov 08, 2011 at 12:52 PM

Found a solution:

@syclamoth: Thanks for the help!

This does not update correctly

   Debug.Log(Camera.main.ScreenToWorldPoint(Input.mousePosition))

but this does

   Debug.Log(Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, 1)));

Not sure where the problem is though :( I assume the actual documentation for Input.mousePosition is not 100% accurate and it might return no or out-of-bounds values for the z-component of the Vector3.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Having player object always snap to mouse position 1 Answer

Need explanation for this code 0 Answers

Can't use camera.main [SOLVED] 1 Answer

ScreenToWorldPoint question 1 Answer

ScreenToWorldCoordinates not accurate 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