• 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
5
Question by charlesvi · Sep 21, 2013 at 04:33 PM · convertingmousemoveworldposworldtoscreen

Converting Mouse Position to World. Stationary Camera.

So I'm just trying to get my mouse position into a world position. I've seen a thousand answers where you Camera.main.ScreenToWorld(Input.MousePosition) it doesnt work. the value it returns is the same (the cameas position 10.5,25,10.5)

This is an overhead perspective not first person thing. So how do I get a mouse pos to become a world one?

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

4 Replies

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

Answer by robertbu · Sep 21, 2013 at 04:42 PM

Camera.main.ScreenToWorldPoint(Input.mousePosition) will not work. The issue that you are mapping a 2D coordinate to a 3D world. To make it work, you need to specify the distance in front of the camera as the 'Z' coordinate in the position you want to map. So for example if you wanted the position 10 units in front of the camera:

 var v3 = Input.mousePosition;
 v3.z = 10.0;
 v3 = Camera.main.ScreenToWorldPoint(v3);

You say 'overhead' perspective. As long as you mean straight down, setting the 'z' parameter is easy. If your camera is at an angle with respect to the "ground," then you want to a different approach to map mouse position to world space.

Comment
Add comment · Show 10 · 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 charlesvi · Sep 21, 2013 at 04:57 PM 1
Share

Doesnt really work. I set it to ortho and perspective no change. Tried different Z's and the wrongness changed. Typically I'm registering a click 1 above and 2 to the right (3 world units up 6 over) though as i go more right it just is 1 up. This is on a chess board if that helps, I included the code.

      if (Input.Get$$anonymous$$ouseButtonDown(0))
         {
             //Debug.Log("Click registered");
             
             //Debug.Log(Input.mousePosition);
             Vector3 pos = Input.mousePosition;
             pos.z = 20;
             pos = Camera.main.ScreenToWorldPoint(pos);
             ray = new Ray(pos, Vector3.down);
             Debug.Log(pos);
 
             if (Physics.Raycast(ray, out hit))
             {
                 hit.collider.renderer.material.color = Color.red;
                 //Debug.Log(hit);
             }
             
         }
avatar image robertbu · Sep 21, 2013 at 05:06 PM 0
Share

'pos' will be the world position under the mouse. But this is not a position you want to use for a Raycast(). Ins$$anonymous$$d:

         if (Input.Get$$anonymous$$ouseButtonDown(0))
         {
             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  
             if (Physics.Raycast(ray, out hit))
             {
                 hit.collider.renderer.material.color = Color.red;
                 //Debug.Log(hit);
             }
  
         }
avatar image charlesvi · Sep 21, 2013 at 05:53 PM 0
Share

Tried that for good measure, still the same innacuracie. The only tile that it hits right is tile 7,7 everything else is 1 up 2 over unless its on the edge (x,7) then its one up unless its 7,7. Top row its 2 over unless its within one space of the wall (6,7) then its just one over.

Tiles are 3x1x3 scale wise. Y is down.

avatar image robertbu · Sep 21, 2013 at 05:58 PM 0
Share

The code I've outlined here is right. Not sure what is going on with your code. Are you sure your colliders are aligned with the visible objects? Use a Debug.DrawRay() to display your raycast.

avatar image charlesvi · Sep 21, 2013 at 06:17 PM 0
Share

.< thank you. Collider was scaled up 5,1,3. I thought i set it back but it was on a temp object not the prefab. Now it works as expected.

Thank you again.

Show more comments
avatar image
1

Answer by Unity_scat · Jun 12, 2016 at 07:02 AM

I wanted to test this out but all code doesn't have "hit" defined. Please help.

Comment
Add comment · Show 1 · 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 tyylerr · Jun 22, 2016 at 09:14 PM 0
Share

I had the same problem, not sure if this is right but I just declared my own variable of type RaycastHit. I also declared ray as a Ray, not sure if this matters. Like this:

 if (Input.Get$$anonymous$$ouseButtonDown(0)) 
 {
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     RaycastHit hit;
     if (Physics.Raycast(ray, out hit))
     {
         hit.collider.renderer.material.color = Color.red;
         //Debug.Log(hit);
     }
 }
avatar image
1

Answer by melsov · Mar 10, 2017 at 02:35 PM

This question is old but, in case anyone needs it, there's a way to do this without ray casts.

The last property gives the mouse position projected onto the transform.

     private static Vector3 cursorWorldPosOnNCP {
         get {
             return Camera.main.ScreenToWorldPoint(
                 new Vector3(Input.mousePosition.x, 
                 Input.mousePosition.y, 
                 Camera.main.nearClipPlane));
         }
     }
 
     private static Vector3 cameraToCursor {
         get {
             return cursorWorldPosOnNCP - Camera.main.transform.position;
         }
     }
 
     private Vector3 cursorOnTransform {
         get {
             Vector3 camToTrans = transform.position - Camera.main.transform.position;
             return Camera.main.transform.position + 
                 cameraToCursor * 
                 (Vector3.Dot(Camera.main.transform.forward, camToTrans) / Vector3.Dot(Camera.main.transform.forward, cameraToCursor));
         }
     }



Comment
Add comment · Show 3 · 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 melsov · Mar 10, 2017 at 02:30 PM 0
Share

By the way, this solution assumes that you don't care about the geometry of the collider. If you need a mouse point on a collider and the collider isn't a box that always faces the camera, use ray casts. This gives a point on the plane that contains transform.position with normal vector (transform.position - camera.position).

avatar image FeastSC2 · Nov 07, 2017 at 02:46 AM 0
Share

Is there a way to have it completely static so it can be called easily from a single script?

avatar image BozoCat · Nov 15, 2019 at 10:48 PM 0
Share

Note that this does not work for orthographic cameras.

avatar image
0

Answer by exvalid · Sep 08, 2018 at 04:03 AM

public Camera[] Cam;

public Camera cam1;

public RaycastHit RayHit;

public Ray ray;

public GameObject ObjectHit;

public Vector3 Hitpoint = Vector3.zero;

update ()

{

             Cam = Camera.allCameras;
             cam1 = Cam[0];

             if (cam1 != null)

             {

                 //Cast Ray from Camera through Mouse

                 ray = cam1.ScreenPointToRay(Input.mousePosition);                    

                 if (Physics.Raycast(ray, out RayHit))

                 {

                     ObjectHit = RayHit.transform.gameObject;
                     Hitpoint = RayHit.point;

                     if (ObjectHit != null)

                         Debug.DrawLine(cam1.transform.position, Hitpoint, Color.blue, 0.5f);

                 }

                 //Cast Ray from Camera through Mouse Ends

             }

}

Hitpoint is your new World Coridinates of the mouse pointer. this casts a line from the camera through the mouse pointer. Rember tochange camera to your camera, this is straight from my script Enjoy Exvalid PS YOUR RECAPTCHA-VERIFICATION SYSTEM IS THE WORST THING IVE EVER HAD TO ENDURE IN MY LIFE TO MAKE THIS POST AND EDIT IT REMOVE IT AT ONCE ITS TERRIBLE FIRE WHOEVER MADE IT.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Space Flying Script Using Mouse 1 Answer

MoveTowards? 3 Answers

need help converting JS to C 2 Answers

Cant convert int to string in c# 1 Answer

2D mouse aiming C# 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges