Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Particle System Follow Raycast

Discussion in 'Scripting' started by jlpeyton, Jan 9, 2020.

  1. jlpeyton

    jlpeyton

    Joined:
    Dec 22, 2017
    Posts:
    57
    I have a cube with a raycast that reflects off other objects. I also have a particle system shooting a beam attached to the same cube. How can I apply the same physics to particles?

    raycast.JPG

    Code (CSharp):
    1. private void Reflect(Vector3 position, Vector3 direction, int reflectionsRemaining)
    2.     {
    3.         if (reflectionsRemaining == 0)
    4.         {
    5.             return;
    6.         }
    7.  
    8.         Vector3 startingPosition = position;
    9.  
    10.         Ray ray = new Ray(position, direction);
    11.         RaycastHit hit;
    12.         if (Physics.Raycast(ray, out hit, maxStepDistance))
    13.         {
    14.             direction = Vector3.Reflect(direction, hit.normal);
    15.             position = hit.point;
    16.         }
    17.         else
    18.         {
    19.             position += direction * maxStepDistance;
    20.         }
    21.  
    22.         Gizmos.color = Color.yellow;
    23.         Gizmos.DrawLine(startingPosition, position);
    24.  
    25.         Reflect(position, direction, reflectionsRemaining - 1);
    26.     }
     
    Last edited: Jan 9, 2020
  2. jlpeyton

    jlpeyton

    Joined:
    Dec 22, 2017
    Posts:
    57
    Ok, so I was able to figure out how to render a line following the Raycast and I'll deal with particles later. I also went away from using a recursive function and stuck everything in a For Loop. My only issue now is when I set the positions of the line with the vector array, it wipes out the initial starting positition. How can I store this starting point for the line?

    Code (CSharp):
    1. private void Reflect(Vector3 position, Vector3 direction, int reflectionsRemaining)
    2.     {
    3.         Vector3 startingPosition = position;
    4.         Vector3[] points = new Vector3[maxReflectionCount];
    5.  
    6.         line.SetPosition(0, startingPosition);
    7.  
    8.         for (int i = 0; i < maxReflectionCount; i++)
    9.         {
    10.             Ray ray = new Ray(position, direction);
    11.             RaycastHit hit;
    12.  
    13.             if (Physics.Raycast(ray, out hit, maxStepDistance))
    14.             {
    15.                 direction = Vector3.Reflect(direction, hit.normal);
    16.                 position = hit.point;
    17.             }
    18.             else
    19.             {
    20.                 position += direction * maxStepDistance;
    21.             }
    22.  
    23.             points[i] = position;
    24.         }
    25.  
    26.         line.SetPositions(points);
    27.  
    28.     }
     
    Last edited: Jan 9, 2020
  3. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    You could make 'points' one larger, make the 0th entry the starting point, and then start your for loop at i=1 instead and end at maxReflectionCount + 1.
     
  4. jlpeyton

    jlpeyton

    Joined:
    Dec 22, 2017
    Posts:
    57
    That worked! Thank you.