4
\$\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\$
  • 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 Dec 10 '17 at 2:12
0
\$\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\$
  • 1
    \$\begingroup\$ Or, as the mousepos vector is not being recomposed... object.transform.position = mousepos \$\endgroup\$ – Draco18s Dec 11 '17 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 Dec 11 '17 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\$ – Draco18s Dec 11 '17 at 0:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.