Search Unity

help with screentoworldpoint

Discussion in 'Scripting' started by bchen5803, May 25, 2015.

  1. bchen5803

    bchen5803

    Joined:
    May 25, 2015
    Posts:
    11
    I'm new to Unity and I'm following a book I bought called Intro to game design, prototype and development. It teaches game dev in Unity. In one of the examples the author was not specific about the screentoworldpoint.
    He gave a pretty vague description of it and I searched all over youtube and the unity scripting reference. I still do not get what it does. All I know is that it converts the point from 2d screen space into 3d game world space from the comment in the book.

    I have no clue what that means at all. I just know that it takes a vector3 as parameter and you must explicitly define the z position for that vector3

    I am actually really slow so I'm hoping you guys can give me a really good example

    Thanks !
     
  2. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Rather than much things up by trying to explain it myself, I found a previous user who had asked the same question and who received a rather well thought out answer from robertbu... You'll find the original Q&A here: http://answers.unity3d.com/questions/412010/need-help-with-screentoworldpoint-c.html

    To save you a little clicking, I'll quote the answer here for you as well:

    "An experiment might be the best way to understand this function. Stand at the center of a window and look straight ahead. The amount of the world you can see will depend on the distance from the window. Near the window, you may only see 20' to either side. In the distance you may see a mountain range spanning 10's of miles. Now close one eye and touch the window in a place that aligns with an outside object. How far that object is left or right of you will depend on the distance it is from the window.

    ScreenToWorldPoint() is like the finger on the window. The 'Z' parameter you specify is the distance in front of the camera. It takes a 2D position on the screen (like the position of the finger on the window), and finds what world coordinate position your 'finger/mouse' would align with at the specified distance.

    As for another example, I posted a script for this question that uses ScreenToWorldPoint():
    http://answers.unity3d.com/questions/411419/rotationy-flips-when-turning-around-x-axis.html
    "