Smoothing collision rate
Hi there ! I have that script that plays an audio clip, and instantiate a particle effect, on collision point, between two moving gameobjects.
Each time the two gameobjects collide, 5 to 10 clones of the particle effect are spawned, like a continuous detection each frames, wich causes huge fps drops at some points.
Is there any way to add some sort of "lazyness" to the collision detection rate only for these two gameobjects ?
public ParticleSystem particleSystem;
public AudioClip soundClip;
public AudioSource audioSource;
public float volume = 1.0f;
void OnCollisionEnter(Collision collision){
audioSource.PlayOneShot (soundClip, volume);
ContactPoint contact = collision.contacts[0];
ParticleSystem clone = (ParticleSystem)Instantiate (particleSystem, transform.position, Quaternion.identity);
}
}
Feel free to say anthing about the script, i'm pretty new to this.
Answer by danielrothmann · Jun 28, 2017 at 01:25 PM
Hi @Cepeka ,
In the past I've had luck with testing for the velocity of the collision, so your code will only be triggered if the collision is strong enough - For example:
void OnCollisionEnter(Collision collision){
if (collision.relativeVelocity.magnitude > 2)
{
audioSource.PlayOneShot (soundClip, volume);
ContactPoint contact = collision.contacts[0];
ParticleSystem clone = (ParticleSystem)Instantiate (particleSystem, transform.position, Quaternion.identity);
}
}
Your answer
Follow this Question
Related Questions
Selective Collision Detection 2 Answers
My collision is not detecting 1 Answer
Best way to do Collision detection when you have multiple enemies/allies around you 0 Answers
Can I detect a collision between two triggers on other objects 1 Answer
How to detect a collision. Coming back to using Unity after taking a break for a few years. 0 Answers