- Home /
Generating a particle effect on top of a GUIElement
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.
That does not work either, i tried it before this, sorry should have mentioned that.
Answer by rutter · Jul 08, 2014 at 08:05 AM
The camera class has a bunch of helper functions for translating points to/from "screen points" (in pixels) and "viewport points" (which GUITextures use) and "world points" (in the scene itself).
In particular, it sounds like you might want ViewportToWorldPoint. Be careful to set a z-component for your input, which represents the desired distance away from the camera.
When in doubt, Camera.main is usually the player's main camera.
Something like this:
Vector3 worldPos = Camera.main.ViewportToWorldPoint(SOME_GUI_POSITION + Vector3.forward);
...and this is the "GUIElements using a different coordinate system" mentioned by the OP.
Clearly, you have to translate GUI coords to regular coords, and this is what does it.
This seems to spawn it in the wrong position, not where the GUI is located... $$anonymous$$y code is as follows:
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;
Your answer
Follow this Question
Related Questions
Syncing particle system in Photon Unity Networking 1 Answer
How to Instantiate gameObject where a particle dies 2 Answers
How to put a particle system on each of Text Mesh Pro characters? 0 Answers
Best way to spread particle system? 1 Answer
How do I make instantiated particles inherit Transform movement from their parent object? 1 Answer