3
\$\begingroup\$

How can I place a object to position where I clicked in just x,y axis not in z, I am trying to achieve it with

Vector3 mousepos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
object.transform.position = new Vector3(mousepos,mousepos);

but it does not seem to work, in fact the object doesn't even move.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ new Vector3(mouspos,mousepos); This isn't valid syntax. There's no Vector3 constructor that takes two Vector3s as arguments. What is it you're trying to do with this? \$\endgroup\$
    – DMGregory
    Commented Dec 10, 2017 at 2:12

1 Answer 1

-1
\$\begingroup\$

You need to reference the value, not the entire vector.

 Vector3 mousepos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 object.transform.position = new Vector3(mousepos.x,mousepos.y,0);
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Or, as the mousepos vector is not being recomposed... object.transform.position = mousepos \$\endgroup\$ Commented Dec 11, 2017 at 0:38
  • \$\begingroup\$ I was pretty sure that was the case, but couldn't remember what the Z value would be on it \$\endgroup\$
    – Stephan
    Commented Dec 11, 2017 at 0:42
  • 2
    \$\begingroup\$ Depends on your scene. IIRC, a ray is fired into the scene and returns the first point it hits a collider (or the camera's far plane). Ah, I was wrong. As Input.MousePosition will have 0 as the z component, then this method will only ever return the position of the camera. \$\endgroup\$ Commented Dec 11, 2017 at 0:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .