Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting mouse position in unity

Tags:

c#

unity3d

I'm trying to move a object to the mouse position. But it's giving me large x value like 300 but at that place the pre placed object's x position is -4.

rigidBody.velocity = new Vector3(Input.mousePosition.x, EndPointY, 0)*4;

So how can I get the current mouse position?

Thank you..

like image 504
Bucky Avatar asked Oct 29 '17 08:10

Bucky


People also ask

How to convert the mouse position to world space in Unity?

How to convert the mouse position to world space in Unity (2D + 3D) In Unity by JohnJanuary 22, 2020 18 Comments. In Unity, getting the mouse position on the screen is fairly straightforward. It’s a property of the Input class so, to access it from a script, all you need to do is use Input.mousePosition, which returns the position of the mouse, ...

Do you need to translate mouse position in Unity?

Whatever it is, you’re going to need to translate the mouse’s position on the screen, which is measured in pixel coordinates, into a real position in the world that Game Objects use. So… How do you get the mouse position in world space in Unity?

How to get the position of the mouse on the screen?

Input.mousePosition will give you the position of the mouse on screen (pixels). You need to convert those pixels to the world units using Camera.ScreenToWorldPoint (). You can follow this link to learn how to drag a 3d object with the mouse or you can copy this code to move an object from the current position to the mouse position.

What is the mouse position in the input class?

However, Input.mousePosition is a Vector3 value, meaning that it also has a third Z value, which can cause some problems. The Mouse Position property of the Input Class uses Unity’s default method of input, the Input Manager. However, if you’re using Unity’s new Input System, you’ll need to use a different method for capturing the mouse position.


2 Answers

The conversion with ScreenToWorldPoint is straight-forward for 2D Vectors:

    Vector2 screenPosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
    Vector2 worldPosition = Camera.main.ScreenToWorldPoint(screenPosition);
like image 128
marts Avatar answered Sep 19 '22 17:09

marts


Vector3 MousePosInWorldSpace()
{

   return Camera.main.ScreenToWorldPoint(Input.mousePosition);

}

ScreenToWorldPoint returns a point in world space, at the provided z distance from the camera.

Since Input.mousePosition.z position is always 0, in the above example, the z value of the returned point would equal to the z position of the camera.

In the following example Camera.main.nearClipPlane distance is provided as the z distance from camera for the returned world space point. The camera cannot see geometry that is closer to this distance.

Vector3 MousePosInWorldSpace()
{

   Vector3 mousePos = Input.mousePosition;
   return Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, Camera.main.nearClipPlane));

}
like image 27
Gayan Weerakutti Avatar answered Sep 19 '22 17:09

Gayan Weerakutti