- Home /
Instantiate Game object at clicked position?
So I am attempting to spawn my particle effect at the clicked mouse position using the code below:
void OnMouseDown () {
var screenPos = Input.mousePosition;
var worldPos = cam.GetComponent<Camera>().ScreenToWorldPoint(screenPos);
if (timing == true) {
Instantiate(clickEffect,new Vector2(worldPos.x,worldPos.y),Quaternion.identity);
Debug.Log(worldPos);
}
}
The particles spawn and play okay, but they only every spawn in the middle of the screen, not where I click. The debug prints "(0.0, 1.0, -10.0)" no matter where I click. The z being at -10 is fine, but the x and y never change.
I'm not fully clear on converting screen coordinates to world but from what I've read elsewhere I thought this was right, what have I done wrong?
Any help would be greatly appreciated, thanks
This depends a lot on where you actually want to spawn the particles, do you have a plane they spawn on? Just clicking on screen wont tell the game how far into the 3D world it should spawn, this would require a Raycast if the camera has perspective. If it's orthographical I believe this should be working already?
Answer by Eudaimonium · May 12, 2015 at 01:08 PM
You made "worldPos" variable but never actually use it:
Correction:
Instantiate(clickEffect, worldPos, Quaternion.identity);
Answer by hbalint1 · May 12, 2015 at 12:20 PM
I don't think OnMouseDown is the right event you want to use. Put your code snippet in the Update Method and use Input.GetMouseButton(0) instead. as the docs says http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html is for clicking GUI elements and Colliders.
void Update() {
if(Input.GetMouseButton(0)){
var screenPos = Input.mousePosition;
var worldPos = cam.GetComponent<Camera>().ScreenToWorldPoint(screenPos);
if (timing == true) {
Instantiate(clickEffect,new Vector2(screenPos.x,screenPos.y),Quaternion.identity);
Debug.Log(worldPos);
}
}
}
I don't understand why did you calculate the worldPos if you don't use it anyway.
He might be clicking a plane, but yeah he didn't use worldPos.
Sorry my mistake, I wasn't using the worldPos, I have edited. However even when i do use it, the particles still only spawn in middle of the screen. And yes I was using a plane with a box collider, It seemed pointless to constantly check if mouse button was down
oh. i thought you can click anywhere (above or below the plane), sorry. Are you sure "cam" is the camera are you using?
Answer by xortrox · May 12, 2015 at 01:41 PM
After you clearified that you did in fact use a plane with a box collider, the solution is easier if you use a raycast:
void OnMouseDown ()
{
var screenPos = Input.mousePosition;
var ray = cam.GetComponent<Camera>().ScreenPointToRay(screenPos);
if (timing == true)
{
RaycastHit hit;
if(Physics.Raycast(ray, out hit, Mathf.infinity))
{
if(hit.collider.name ="NameOfPlane")
{
Instantiate(clickEffect,new Vector2(hit.point.x, hit.point.y), Quaternion.identity);
Debug.Log(ray);
}
else
{
Debug.Log("Plane was clicked but raycast hit something else.");
}
}
else
{
Debug.Log("Plane was clicked but raycast missed.");
}
}
}
Also the ideal solution to this might be to use OnMouseUp for expected behaviour by your users (It's not normal to have a click happen before you release the button in most cases).
Also sorry for the poor indentations, not sure how to "tab" in code blocks on here yet.
Unfortunately this did not seem to work, there was no errors but nothing happened upon click. However I should have mentioned that Raycast seems unnecessary because this script is attached to the Plane with the box collider which the player clicks. Basically this object gets instantiated where ever the player clicks on the screen. The camera does not ever move either.
$$anonymous$$ake sure ti$$anonymous$$g is true and that the cam variable is even assigned, even if the camera never moves you will have to translate the click to 3D coordinates if the camera has perspective on it. The easiest way to do that would be to use a raycast.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Problem with ScreenToWorldPoint and touches (C#) 2 Answers
Distribute terrain in zones 3 Answers
Instaniate Prefab 1 Answer
C# 2d Instantiate object randomly around gameobject 1 Answer