- Home /
how can i instantiate a GameObject in mouse position and in a special distance from the camera?
i want to instantiate a GameObject in mouse position and i can use camera.ScreenToWorldPoint or ScreenPointToRay methods for finding the world position. i can not use raycasting in my situation. so i need a method to find what is the world position of mouse in (for example 100m away) from the camera position. because camera view is perspective and the view is a cone some calculations are needed. as i know i should take the difference of mouse position from center, into account and use the fieldOfView value to find the angle of the line that i should go on to find the position but i can not produce working code. my code is like this inside Update() function.
if (Input.GetMouseButtonDown(0))
{
Vector3 a = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x,Input.mousePosition.y,0));
a.z = 0;
print(a.z.ToString());
Instantiate (fairy,a,Quaternion.identity);
}
the camera is placed at (0,0,-10) and i want to create fairies at z position of 0.
i have a related question as a featured question too.
If the mouse was always at the center of the screen it would have been easy.You find a vector of where you are looking at + 100m depth from your current position. But In your case it is a bit of a pain for small brains like $$anonymous$$e . In purely geometrical terms you need to find a vector from your camera to the mouse and then on the vector axis to go 100m in depth. And Then you have to eli$$anonymous$$ate the bloody perspective !!! but do you need to eli$$anonymous$$ate perspective ? The horizontal plane from the center of the camera is not distorted.or not ??? Challenging ... If I was a bit more knowledgeable...
Answer by duck · Apr 13, 2010 at 08:45 AM
Use Camera.ScreenToWorldPoint.
To use it, you provide it with a Vector3. The X & Y components of the Vector3 represent the pixel coordinates, measured from the bottom-left of the screen. The Z component represents the distance from the camera in world-units.
For example:
// instantiate an object at the mouse's position, at 20 units away from the camera
var screenPos = Input.MousePosition;
screenPos.z = 20;
var worldPos = camera.ScreenToWorldPoint(screenPos);
var newInstance = Instantiate(prefab, worldPos, Quaternion.identity);
unfortunately it don't work. the position that ScreenToWorld point gets to me is at most my current x/y -1 or +1 and in 20 meters away it's much near the center and the point that mouse is pointing to is my x + 5. can you get the point. we need to rotate the line between ScreenToWorld's result and z+20. i think it should be rotated dipending on mouse pos and fieldofview.
It does work. I just tried it, and objects appear directly under my mouse cursor, at the specified distance away in the scene. Unity does the projection calculations for you, so that the direction radiates away from the camera's position at exactly the right angle so that the object is under the mouse at whatever distance you specify. There must be some other problem in your code, or in the way that your scene is set up. Check that you're converting from the correct camera (if you have more than one cam in your scene), and perhaps you could post your code if you're still having problems.
i edited my answer to include the code. the code is like yours but the result is not. the camera is placed at z pos of -10.
In your code, you're specifying a Z value of zero. This means the object will be created at exactly the same position as the camera (zero units away). Is that what you want? In my example, it positions the objects 20 units away from the camera. I'm having trouble understanding what you want - perhaps the best way to explain it might be to give any examples you can find of any other games which show the behaviour that you want?
my camera's z is -10 so 0 is 10 units away from the camera.
Answer by Ashkan_gc · Apr 14, 2010 at 05:36 PM
duck's answer is correct. thank you man! i write this for clearification. i read the docs carelessly. it says that the third argument of ScreenToWorldPoint method should be the z's distance from camera and not in world/scene space. so when my camera is at z of -10, 0 means 10 meters away from the camera and code be like this.
if (Input.GetMouseButtonDown(0))
{
Vector3 a = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x,Input.mousePosition.y,10));
Instantiate (fairy,a,Quaternion.identity);
}
Your answer
Follow this Question
Related Questions
Ray Over Mouse 1 Answer
using left right keys to turn 1 Answer
Move camera when right mouse button is pressed 3 Answers
Need help on the 3ds max style camera control 0 Answers
Simple Object Dragging 0 Answers