- Home /
Input.mousePosition to ScreenToWorldPoint
Hey I am facing some problems with ScreenToWorldPoint casting, if you could help me.
Vector3 mouspos = new Vector3 (Input.mousePosition.x, Input.mousePosition.z, Input.mousePosition.y);
mouspos = Camera.main.ScreenToWorldPoint(mouspos);
Instantiate (obj, mouspos, Quaternion.identity);
So basically what I want to do, is to use the mouseposition and to instantiate an object only on x,z axis, but it's not working.
If I am just using
Vector3 mouspos =Input.mousepoint;
mous.pos.z = 5.0f;
mouspos = Camera.main.ScreenToWorldPoint(mouspos);
Instantiate (obj, mouspos, Quaternion.identity);
With this snippet of code, it will work, but this will place my object on x and y axis.
Would really need a little bit of help ^_^
Answer by maccabbe · Mar 15, 2015 at 03:53 PM
Try using the following, it creates a ray in world space that lies on the pixel the pointer is on and intersects it with a plane that lies on y=0.
Plane plane=new Plane(Vector3.up, Vector3.zero);
Ray ray=Camera.main.ScreenPointToRay(Input.mousePosition);
float distance;
if(plane.Raycast(ray, out distance)) {
Vector3 point=ray.GetPoint(distance);
Instantiate (obj, point, Quaternion.identity);
}
edit: good catch!
Yes! it's working. Please edit your answear.
Instantiate (obj, mouspos, Quaternion.identity);
change mouspos with point and I will mark your answer as good.
Your answer
Follow this Question
Related Questions
Trying To Find Mouse Position On Tap 1 Answer
Camera ScreenToWorldPoint Instatiating 1 Answer
How to keep the same position of the object instead of camera 0 Answers
collider2D.bounds.Contains not working properly 3 Answers
when mouse click a gameobject ,how can I get the position of mouse click gameobject 2 Answers