OnParticleCollision() on parent script, for a child particle system?
I'm working on a turret asset script update, and trying to add a particle turret to the list of features.
I have the script on the parent, and a child game object with the particle system. I put OnParticleCollision() in the parent script, but its not detecting collision of the particle system, because its on a child gameobject.
It only works if I place the script with OnParticleCollision() on the gameobject with the particle system, doesn't work if its a on a parent. I assumed it would work, because that's how regular compound colliders work.
The turret script has to be on the parent, and the particle collider will in most cases have to be on a child. And for simplicity's sake, I'd prefer to avoid splitting the script into two pieces just so I can have a few lines of code about particle collision, somewhere down the hierarchy on one of the child gameobjects. So can this be done somehow? Keeping the script on the parent, and detecting collisions of the child particle system?
PS. Alternatively maybe being able to adjust the particle start position through script would be good too, then the particle system won't have to be on the child game object, but I don't see that option in the documentation.
Anyone?
Basically all I want to know is, does OnParticleCollision have to be on the game object with the particle system? Can it be on the parent of that game object somehow?
Answer by allenallenallen · Oct 19, 2015 at 03:57 PM
I don't believe there's a workaround without splitting the work between the parent and child objects.
Each OnParticleCollision() is for the collider of that GameObject or the Particle System that script is attached to. So like you suspected, the OnParticleCollision() in the parent will be trying to get collision information for the parent object without caring about its child object.
Is there a reason you want to call OnParticleCollision() on the child object? Can it not be called on the object that the particles will be colliding into?
Yes theres two reasons. One is because there could be multiple particle systems on the same turret, and the other is, they need to be positioned at the tip of the barrel, so they always face where the turret head is facing, and their position to be where the tip is.
But since there's no "particleOrigin" or something variable for it, and the script that handles the entire system is at the root(parent) of the turret, there's no other option than to have a small script of few lines of code on each particle system, that receives and passes on the damage value. I just didn't want the users to go through the hassle of placing that, since all other types (raycast, projectile, melee, shock, laser), and all other aspects of customization are done from the topmost parent, of all the parts.
I think I'll settle for whenever the user adds a particle system to the particles slot, it'll add that script too (through add component), without their need to do it personally.
Thanks for clearing it up. Here's the asset in question. I'll give you both a voucher for trying to help, if you need it. Just tell me where to send. :)
Answer by iyenal222 · Oct 19, 2015 at 03:17 PM
This can help:
http://docs.unity3d.com/ScriptReference/ParticleSystem.Play.html
Use public void Play(bool withChildren = true);
on a parent gameobject to play a child particle system but I have tested it and he give me errors :-(, maybe you have more luck...
No I have no issues playing or anything. Its the particle collision that doesn't work if the script with OnParticleCollision() is on parent, and not the child gameobject with the particle system.
Like this -Parent (with script that contains OnParticleCollision) ->>Child (with the Particle System I'm trying to get collision info from)
Because if it was regular Parent with rigidbody, and child with collider, it would work. But particle collision doesn't work like that I guess? So I have to put a seperate script on the child just to get collision from. :P
I'm wondering if that's how its meant to be, and if there's a workaround for it. :)
Hey I can give you a voucher for the asset too, if you need it. For trying to help. Just tell me where to send. :)
Your answer
Follow this Question
Related Questions
Getting Parent object from a child's collision. 1 Answer
player parent is killed when sword child hits enemies 2 Answers
How could I make the children of an object face the direction it is moving on the y-axis? 0 Answers
How can I reattach the player transform after using DetachChildren() to flip the parent? 2 Answers
Calculate child local position within a parent GO without a child, given a world position..? 0 Answers