- Home /
Collision on zero-velocity particle
This one has me stumped a little. I've figured out that in order to use .OnParticleCollision()
your particles need to have some velocity.
The problem with that is that I'm trying to make stationary coin pickups. I don't want to give them velocity as then they start moving.
Does this mean that I can't use particle collisions in this case?
One possible - but ugly - avenue of thought is to counter the particle velocity by moving its parent in the opposite direction at exactly the same speed as the particles, thus giving the illusion that they're not moving. I'd rather not do that though.
Any and all insights much appreciated! :)
Answer by allenallenallen · Aug 10, 2015 at 06:14 AM
Pretty sure OnParticleCollision() doesn't require velocity. Where did you get that information?
I read it somewhere and can't find it now. Will keep looking.
$$anonymous$$y setup is a particle system inside a parent. The parent is moving by setting its position, not using physics (which makes the particles move with it). $$anonymous$$y collision detector also doesn't move.
If I set the velocity of the particles to Vector3.zero I register no collisions. If I set the velocity to Vector3(0,0,-0.01) I get collisions.
I could set up a little proof-of-concept scene if it helps?
After testing OnParticleCollision() myself, I can't replicate your problem. It works fine on my end. And I didn't add any velocity whatsoever.
I made two cubes, one with particle system as child. I moved the parent cube closer to the other cube and got log Particle Hit.
Okay, I've made a scene to illustrate. Definitely doesn't work for me. Do you have the "Start speed" set to 0 and also no extra velocity added to particles otherwise? I.e. a stationary particle.
With velocity added collisions get logged:
Without velocity no collisions get logged:
Here's the Unity project if anyone wants to have a look. To make the collisions work you can simply tick "Velocity over lifetime" and it'll start working.
Ah, I see what you meant. I thought you meant external velocity.
You see, a particle system only works when the particles are actually moving using the Particle System component. If you're just spawning random particles in midair without it moving, then it's not behaving like one.
So yes, you do need some sort of INTERNAL velocity/movement/acceleration for the particles to detect collision.
Answer by pea · Aug 10, 2015 at 09:53 AM
Seeing how particles need some internal velocity to act as a particle I've decided to try the following approach. It gives particles minimal velocity and reverses the velocity every 0.5 seconds. This makes the particles seem like they're stationary. But in actual fact they're ever so slowly moving in alternate directions. And this makes collision detection work.
This script goes on the game object that contains the particle system. I'm not too sure about the performance overhead of this! But I tried to minimise overhead by only updating every 0.5 seconds.
using UnityEngine;
using System.Collections;
public class particleVelocity : MonoBehaviour {
private ParticleSystem s;
private ParticleSystem.Particle[] parts;
private Vector3 velocity = new Vector3(0,0,0.01f);
void Start() {
this.s = GetComponent<ParticleSystem>();
InvokeRepeating("updateVelocity", 0, 0.5f);
}
private void updateVelocity() {
this.velocity = -this.velocity;
this.parts = new ParticleSystem.Particle[this.s.particleCount+1];
int l = this.s.GetParticles(this.parts);
int i = 0;
while (i < l) {
this.parts[i].velocity = this.velocity;
i++;
}
this.s.SetParticles(this.parts, l);
}
}
Your answer
Follow this Question
Related Questions
Why can't I add non-prefab colliders to a particle effects triggers list? 0 Answers
Shuriken collision with terrain? 0 Answers
Getting the position/intersection of collided particles 1 Answer
Getting particles from the same system to act differently with different objects? 0 Answers
Collide with Non-Moving Particle 0 Answers