- Home /
Detecting collision between particle and player.
Hello,
I am trying to make my game detect the collision between pepper gas smoke particles and my player, and if such collision occurs, apply some damage to the player. But as I have spent my last 2 days, although I can see the collision happening in the game scene, the collision has never been detected. I use Shuriken particle system. I checked the collision in the inspector, and both the player and the particle system has the RigidBody physic.
My code goes as:
#pragma strict
var P : GameObject;
var HitByGaz : boolean = false;
function OnParticleCollision (other : GameObject) {
Debug.Log ("Pepper gas!");
if(other.tag == "Player") {
P.gameObject.GetComponent(PlayerHealth).currentHealth -= 1;
HitByGaz = true;
HitByGaz = false;
}
}
I can't even get the log working. Thanks for helping out.
Answer by Hasi_Indie · Dec 25, 2013 at 05:30 PM
#pragma strict
var player : GameObject;
function OnParticleCollision (other : GameObject) {
var body : Rigidbody = other.rigidbody;
if (body) {
Debug.Log("player");
}
}
works for me... if not working set your collision quality to high.
Answer by AD110 · Jan 17, 2017 at 02:22 PM
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Example : MonoBehaviour
{
public ParticleSystem.Particle[] ParticleList;
private ParticleSystem m_particleSystem;
private GameObject character;
int Score = 0;
void Start()
{
character = GameObject.FindGameObjectWithTag ("Iz");
m_particleSystem = GetComponent<ParticleSystem> ();
}
void LateUpdate ()
{
//This list will be used to store each particle that is alive
ParticleList = new ParticleSystem.Particle[m_particleSystem.particleCount];
//storing particles into a list
m_particleSystem.GetParticles (ParticleList);
// Change only the particles that are alive
for (int z = 0; z < GetComponent<ParticleSystem> ().particleCount; z++)
{
//getting the position of the player and the particle
Vector3 dis = ParticleList [z].position - character.transform.position;
//if it is near
if(dis.magnitude <= 5)
{
//destroy the particle
ParticleList [z].lifetime = 0;
//add score
Score++;
}
}
// Apply the particle changes to the particle system
m_particleSystem.SetParticles (ParticleList, m_particleSystem.particleCount);
}
}
Your answer
Follow this Question
Related Questions
Inconsistent Particle Collision detection 0 Answers
How do I use compute shaders with particles? 0 Answers
OnCollisionExit for Particles 0 Answers
[2D] Getting particles to collide with a game object(Explosion debris effect) 0 Answers
How to detect which exact particle element from a particle system hit a collider? 0 Answers