Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How do I detect a collision between a game object and a particle?

Discussion in 'Scripting' started by dusthound, Aug 7, 2019.

  1. dusthound

    dusthound

    Joined:
    Sep 18, 2018
    Posts:
    98
    I am working on a 2D game where I am using a particle system to generate projectiles from the players weapon and now need to detect when a particle collides with an enemy GameObject. I have tried using the OnParticleCollision function to detect collisions but can't get it to work. I have tried attaching the function to both the particle system and the enemy GameObject but neither has worked. When I attached the OnParticleCollision function to the particle systems script the particles stopped colliding with the enemy GameObject and passed through it instead. I have send collision messages enabled on my particle system as well as the collision module. The collision type is set to world and the mode is set to 2D. I also have enable dynamic collisions on. I have tried looking for help in the API page for OnParticleCollision but haven't been able to figure it out. I am not sure what to do next to solve this problem and need some help.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Are you sure you need to use the particle collisions as your projectile? Those messages are sent for every single particle, which seems like overkill for a weapon system. I would attach the particle system to a GameObject with an invisible trigger or collider and use that for your collision callbacks.
     
  3. dusthound

    dusthound

    Joined:
    Sep 18, 2018
    Posts:
    98
    I figured this method was more efficient than spawning in a new GameObject every time the player shoots