16

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..

asked Oct 29, 2017 at 8:30

5 Answers 5

20

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.

 //the object to move
public Transform objectToMove;
 void Update()
 {
 Vector3 mouse = Input.mousePosition;
 Ray castPoint = Camera.main.ScreenPointToRay(mouse);
 RaycastHit hit;
 if (Physics.Raycast(castPoint, out hit, Mathf.Infinity))
 {
 objectToMove.transform.position = hit.point;
 }
 }
Fjut
1,33412 silver badges24 bronze badges
answered Oct 29, 2017 at 10:44
Sign up to request clarification or add additional context in comments.

Comments

11

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);
answered May 1, 2020 at 18:23

Comments

6

That is the current mouse position. The issue is that your objects are in world coordinates and the mouse is using screen coordinates.

You need to convert the mouse position using Camera.ScreenToWorldPoint().

answered Oct 29, 2017 at 8:57

Comments

5

If you wanted to use the new Unity InputSystem you could do the following:

using UnityEngine;
using UnityEngine.InputSystem;
...
Vector2 screenPosition = Mouse.current.position.ReadValue();
Vector2 worldPosition = Camera.main.ScreenToWorldPoint(screenPosition)

Note that you need to install the InputSystem package for this to work.

answered Aug 16, 2020 at 15:29

Comments

1
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));
}
answered Sep 11, 2021 at 12:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.