Search Unity

mouse: position: screen to world coordinates

Discussion in 'Scripting' started by renman3000, Nov 21, 2011.

  1. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Is there a command that will convert this? Of course, understanding that it is moving from 2D to 3D, (perhaps I can chose the axis').

    I noticed a few in reference but none precisely for what I want unless, hopefully I missed it.



    Thanks
    Ren
     
    randallrmz likes this.
  2. Ted-Chirvasiu

    Ted-Chirvasiu

    Joined:
    Sep 7, 2010
    Posts:
    381
    DMorock, sean244, phdornheim and 2 others like this.
  3. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Ok.
    Yes, I sw that one but was hestent to use it since it had a Camera pointer.
    But I tried it out, however my suspicions were confirmed, at least for what I am doing...

    ///
    first I tried to pop this into my players controls...
    Code (csharp):
    1.  
    2. function Update()
    3. {
    4.     if(Input.GetMouseButtonUp(0))
    5.     {
    6.         var p : Vector3 = Camera.ScreenToWorldPoint (Input.mousePosition);
    7. }
    8. }
    9.  
    ... But since it tries to access Camera and none exists on this body.

    ///
    so, I removed "Camera" and replaced it with rigidbody. No dice then transform. No dice.

    ///
    So, I moved it into the camera follow script... however I get the message.
    Unsure what to make of it.



    :?
     
    Last edited: Nov 21, 2011
  4. Ted-Chirvasiu

    Ted-Chirvasiu

    Joined:
    Sep 7, 2010
    Posts:
    381
    If you attach that script to something else than the camera,you will have to call it as Camera.main,else if you call it just "Camera" it wont work.And Tag your main camera with "Main camera".Or you can try to call it from a variable,but you will need to define it as var camera1 : Camera; and assign it in your editor.

    http://unity3d.com/support/documentation/ScriptReference/Camera-main.html

    This is why you got that error.
     
    Last edited: Nov 21, 2011
  5. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Ok, great I got it working, thank you. Camera.main, was what I needed to call.

    It is a bit disappointing tho since no matter where I click, "camPos2MousePos" == "camera.position". This is not what I want. I want/expect the camPos2MousePos to vary dependant on the mouse clicked position.


    Unsure.
    Code (csharp):
    1.  
    2.  
    3.     if(Input.GetMouseButtonUp(0))
    4.     {
    5.         var camPos2MousePos : Vector3 = camera.main.ScreenToWorldPoint (Input.mousePosition);
    6.         print("camPos2MousePos " + camPos2MousePos);
    7.         print("camera.position " + camera.main.transform.position);
    8.         print("Input.mousePosition (pixels coordinates) " +  Input.mousePosition);
    9.         print("transform.position (camera position world space)" + transform.position);
    10.     }
    11.  
     
  6. Ted-Chirvasiu

    Ted-Chirvasiu

    Joined:
    Sep 7, 2010
    Posts:
    381
    Aw,then if your z coord is not defined,you will need to use this :

    var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    http://unity3d.com/support/documentation/ScriptReference/Input-mousePosition.html

    http://unity3d.com/support/documentation/ScriptReference/Camera.ScreenPointToRay.html

    (A raycast)

    And use http://unity3d.com/support/documentation/ScriptReference/RaycastHit.html to determine where the raycast hit the object


    Code (csharp):
    1.  
    2. var particle : GameObject;
    3. function Update () {
    4. if (Input.GetButtonDown ("Fire1")) {
    5. // Construct a ray from the current mouse coordinates
    6. var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    7. var hit : RaycastHit;
    8. if (Physics.Raycast (ray,hit)) {
    9. // Create a particle if hit
    10. Instantiate (particle, hit.point, transform.rotation);
    11. }
    12. }
    13. }
    14.  

    Good luck!
     
    Last edited: Nov 21, 2011
  7. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Hmm,
    thanks. I went over it, tested the command but i get something peculiar returned regarding the rays direction.


    For instance, I have my camera motionless. I click the top left corner and it returns a value of the ray direction of (-0.2, 0.3, 0.9). Then I click the top right, and I get a reading (0.2, 0.3, 0.9). How could this be?? How come it returns exact, precise, reflected values when in fact I am clicking un-mirroring vectors?
     
    Last edited: Nov 21, 2011
  8. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
  9. Ted-Chirvasiu

    Ted-Chirvasiu

    Joined:
    Sep 7, 2010
    Posts:
    381
    It's easy.It is actually mirroring on the x coord.Its origin is from the camera and its destination is where it hits.
     
  10. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    I understand that. My question is based on this...
    For instance, say I first click at pos (-2,1,0) it would read (again, example, not at computer) direction(-2,1,0). Then I click ( 3, 3, 0) and it will print direction (2,1,0)


    This does not make any sense unless rays only move in quadrants, which I doubt. I know I must be missing something. Someone please explain.



    Thanks.
     
  11. Ted-Chirvasiu

    Ted-Chirvasiu

    Joined:
    Sep 7, 2010
    Posts:
    381
  12. hongouru

    hongouru

    Joined:
    Jan 22, 2016
    Posts:
    2
    use Camera.main.ScreenToWorldPoint(Input.mouse.Position)