1
\$\begingroup\$

I am working in Unity on a 2D project which is using C#.

I have a number of guitextures which are hearts in order to create a sort of life meter. I would like when the player loses a heart for the heart to change into a skull in a puff of smoke. I currently have the hearts correctly turning into skulls, and a puff of smoke is generated, however the smoke is created in the wrong location.

The smoke is currently generated by:

GameObject smokeObj = (GameObject)Instantiate (smoke, transform.TransformPoint(transform.position), Quaternion.identity);

I have also tried:

GameObject smokeObj = (GameObject)Instantiate (smoke, transform.position, Quaternion.identity);

I know that the reason that the smoke is created in to wrong location is due to GUIElements using a different coordinate system than other gameObjects.

How can I create the smoke in the correct location?

This is a 2D project and I am currently using Unity 4.5of6, with windows 8.1.

Thank you for your assistance, if you have any questions please do not hesitate to ask.

EDIT: I have also tried the following, yet it also works incorrectly:

Vector3 worldPos = cam.ViewportToWorldPoint(transform.localPosition + Vector3.forward); 
        worldPos.z=0;
        GameObject smokeObj = (GameObject)Instantiate (smoke, worldPos, Quaternion.identity);
        Debug.Log(worldPos);
                //smokeObj.layer = 5;
                smokeObj.transform.parent = this.transform;
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You can convert viewport space coordinates into world space coordinates using Camera.ViewportToWorldPointSwitch and use the returned position to instantiate your smoke particles at correct position.

\$\endgroup\$

You must log in to answer this question.

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