- Home /
Is there OnParticleCollisionExit()? If none how to make.
I made a weapon that throwing particles as bullet. If the particle(weapon) collide to the enemy obviously the enemy will decrease it health and a Hit_Enemy animation will play. My problem is I cant change the animation into Run_Enemy because I cant exit the particle collision.
Sample Code:
void OnParticleCollision() {
//Debug.Log (currentHealth);
enemy_animation.SetBool ("isHit", true);
enemyCurrentHealth -= PlayerDamage;
if (enemyCurrentHealth < 0)
StartCoroutine (Died ());
else
UpdateHealthBar ();
}//on particle collision
I want to change the animation if the particle exit to the enemy collider. I hope you can help me. Thank you.
Answer by Mr_Kaizoku · Aug 29, 2018 at 07:51 PM
I know it's pretty late, but if someone searching i want to help him.
Well as i search for it but didn't find any. So i came with my own, i don't know if its a great answer but it work well.
public class ParticleBehaviour : MonoBehaviour {
public static bool PARTICLE_COLLISION = false;
private void LateUpdate() {
PARTICLE_COLLISION = false;
}
private void OnParticleCollision(GameObject other) {
PARTICLE_COLLISION = true;
}
}
and now you can check it in a object script with which you want to make collision do this is update()
private void Update() {
if (!ParticleBehaviour.PARTICLE_COLLISION) {
// now you can write your code for after collision ends
}
}
Answer by MBrown42 · Feb 07, 2020 at 07:12 PM
My game had slightly different requirements than knowing when in theory a particle or stream "exited". For me, the situation is that a stream of repair particles hits something that needs repair, I needed to know when it stopped. In a nutshell, I overwrite a timestamp of the last time a particle hit. Then in fixed update, I checked if it's been longer than a threshold and if so, deem the particle collisions have stopped.
Hopefully this is useful to someone. Thanks! Matt
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BreakableRepairableThingPiece : BRTChild
{
// Have particles collided?
public bool m_bParticlesCollided = false;
// When did particles start colliding
public float m_fRepairParticleStartCollisionTime;
// How long do we go without particle collision before saying we are not?
public float m_fRepairParticleNoCollideTime = 0.2f;
// For future scalability, currently unused
public int m_iLastLayerCollided;
// we get hit with a particle
void OnParticleCollision(GameObject _otherGO)
{
// We only track last particle collided info for layer 16 (repair particles)
if (_otherGO.layer == 16 )
{
// Record / Overwrite the time we just got hit with a repair particle
m_fRepairParticleStartCollisionTime = Time.time;
// Set our particles colliding flag to true
m_bParticlesCollided = true;
// For future scalability, currently unused
m_iLastLayerCollided = _otherGO.layer;
}
myBRT.registerParticleCollision(_otherGO);
}
// Every frame check if we have gone long enough without getting hit by repair particles
// To deam ourself no longer under repair
public void FixedUpdate()
{
// If it's been long enough
if (m_bParticlesCollided && Time.time - m_fRepairParticleStartCollisionTime >= m_fRepairParticleNoCollideTime)
{
// Set our particles colliding flag to false
m_bParticlesCollided = false;
// Tell our parent that we are no longer getting repaired
myBRT.RegisterParticleCollisionStop(m_iLastLayerCollided);
}
}
}
,