- Home /
Question by
m_pangolin · Jan 04, 2015 at 02:41 PM ·
particlesystemonparticlecollision
Rain drops need to hit floor and change texture
I need raindrops to hit the floor and change texture
I've added a ParticleSystem component which drops rain and I've managed to let the drops hit the floor by creating an empty game object on the floor's position.
The problem is changing the texture.
I tried to approach this by OnParticleCollision method but it's not being called and I don't know if that's the solution for my problem.
Comment
Answer by andrew-lukasik · Feb 04, 2015 at 11:12 AM
void OnParticleCollision ( GameObject otherGameObject ) {}
is called on your particle emitting gameObject's scripts, not hit one.
$$anonymous$$ore advanced version of this method (at least best I know so far) would be something like this:
void OnParticleCollision ( GameObject otherGameObject ) {
ParticleSystem myParticleSystem = GetComponent<ParticleSystem>();
ParticleCollisionEvent[] collisionEvents = new ParticleCollisionEvent[myParticleSystem.GetSafeCollisionEventSize()];
myParticleSystem.GetCollisionEvents( otherGameObject , collisionEvents );
foreach( ParticleCollisionEvent itemParticleCollisionEvent in collisionEvents ) {
if( itemParticleCollisionEvent.intersection==Vector3.zero ) return;
Vector3 particleHitPosition = itemParticleCollisionEvent.intersection;
Quaternion particleHitRotation = Quaternion.Euler( itemParticleCollisionEvent.normal );
//
// your code goes here
//
// you can call function on hit collider like this:
// DoStuffClassNameHere getDoStuffClassNameHereComponent = itemParticleCollisionEvent.collider.GetComponent<DoStuffClassNameHere>();
// if( getDoStuffClassNameHereComponent!=null ) getDoStuffClassNameHereComponent.DoStuff();
//
// or change texture on your hit object like this:
// $$anonymous$$eshRenderer get$$anonymous$$eshRenderer = itemParticleCollisionEvent.collider.GetComponent<$$anonymous$$eshRenderer>();
// if( get$$anonymous$$eshRenderer!=null ) get$$anonymous$$eshRenderer.material.mainTexture = new Texture2D( 16 , 16 );
//
}
}