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 to detect collisions with particles in a ParticleSystem

Discussion in 'Scripting' started by soggy_toast, May 3, 2021.

Thread Status:
Not open for further replies.
  1. soggy_toast

    soggy_toast

    Joined:
    Jan 24, 2021
    Posts:
    1
    Hi, so I am making a Tron-like game, where I am using a ParticleSystem to create the trail. I am in the process of making some basic AIs the player can play against. I'd like to be able to Raycast in front of one of my AI characters to determine if they are about to hit a trail, so they can act accordingly. Even though I have Colliders enabled in the ParticleSystem, Raycasting doesn't seem to be working. What can I do to get this working, and is Raycasting on particles possible?

    The Raycasting code I have is as follows:

    Code (CSharp):
    1. RaycastHit[] hits;
    2. hits = Physics.RaycastAll(transform.position, transform.forward, Mathf.Infinity);
    3. for(int i = 0; i < hits.Length; i++) {
    4.     RaycastHit hit = hits[i];
    5.     print(hit.transform.name + " was hit");
    6. }
    I never see the particle system appear in the printed list of registered hits.
     
  2. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    I think this is not possible, but you can add a collider that sticks out in the direction you were raycasting and check if it's triggering the particle system.
     
  3. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,281
    Particles don't automatically get included in the raycast API - you'd need to do something a bit more involved, to detect these.

    The first choice that springs to mind is the Trigger Module in the Particle System.
    You give it a list of colliders and it tells you which particles are interacting with the list of colliders via a script callback. "interacting" can mean inside or outside etc.
     
    karl_jones likes this.
  4. ngocson11a

    ngocson11a

    Joined:
    Sep 19, 2023
    Posts:
    1
    Is there a way to not include the list but still check for collisions?
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520

    Please don't necro-post. If you have a new question, make a new post. It's FREE!!




    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?



    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    - Do not TALK about code without posting it.
    - Do NOT post unformatted code.
    - Do NOT retype code. Use copy/paste properly using code tags.
    - Do NOT post screenshots of code.
    - Do NOT post photographs of code.
    - Do NOT attach entire scripts to your post.
    - ONLY post the relevant code, and then refer to it in your discussion.
     
Thread Status:
Not open for further replies.