- Home /
Getting the position of the mouse in 2D Space?
How can I get this value and either place an object there or move an object towards it?
I'm trying to use Ray currently. But I'm still trying to learn the basics of that.
Answer by talklittle · Jul 12, 2013 at 08:07 AM
http://docs.unity3d.com/Documentation/ScriptReference/Input-mousePosition.html
The example from that page should be close to what you need:
var particle : GameObject;
function Update () {
if (Input.GetButtonDown ("Fire1")) {
// Construct a ray from the current mouse coordinates
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray)) {
// Create a particle if hit
Instantiate (particle, transform.position, transform.rotation);
}
}
}
But instead of just Physics.Raycast (ray), you'll want one of the other versions of Physics.Raycast which gives you hitInfo, so you can find the point where the ray hits.
Answer by SinisterRainbow · Jul 12, 2013 at 08:03 AM
Camera.main.ScreenPointToRay(Input.mousePosition);
Answer by hakermania · Jul 12, 2013 at 08:05 AM
Input.mousePosition will return the position of your mouse in 2D space (x, y)
Moving an object that belongs to 3D space, there, is another story: You should use the screen to world point function so as to translate the position of the mouse to a point into 3D space.
Your answer