[Unity 4.3] Get 2D mouse coordinates relative to the scene

Hello,

I’m new in Unity.

I would like to get the 2D mouse coordinates relative to the scene.

With this code I get the mouse coordinates relative to the camera: Input.mousePosition
The origin is in the left bottom corner.

I would like to set the potition of one sprite to the mouse coordinates :

gameObject.transform.position = Input.mousePosition;

But that doesn’t work :-/.

Any ideas ?

Thanks

Have the same problem, Needs charecter move by mouse press tried this transform.Translate(Input.mousePosition * Time.deltaTime); didn’t worked :frowning:

Use Camera.ViewportToWorldPoint.
[Edit]
quarag is right.

Vector3 pz = Camera.main.ScreenToWorldPoint(Input.mousePosition);
pz.z = 0;
gameObject.transform.position = pz;

11 Likes

Great!

Thanks quarag :wink:

3 Likes

Thanks :slight_smile:

Does anyone know what to do to make a 2d character follow the mouse without clicking anything

Thanks bro, helped a lot :smile:

Thanks a lot this worked :slight_smile:

Use Vector2

Go to quarag’s reply

What does ‘pz.z = 0;’ mean?

Please don’t necro threads like this. This post is 7 years old.

I’ll move this thread to the scripting forum where it belongs.