- Home /
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
}
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).
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?
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.
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));
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.
Your answer
Follow this Question
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