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

Instantiating VFX along RayCast Line

Discussion in 'Visual Effect Graph' started by Rhys_056, Feb 19, 2020.

  1. Rhys_056

    Rhys_056

    Joined:
    Mar 27, 2019
    Posts:
    5
    I created a mechanic to shoot a ball of light along a raycast line, using a particle system GameObject as the projectile. I then went on to create a HDRP VFX, and tried to use this as the gameobject to fire along the raycast, but it does not move forward like the particle system object did. Is there a way to get this to work?
     
  2. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    post screenshot
     
  3. Rhys_056

    Rhys_056

    Joined:
    Mar 27, 2019
    Posts:
    5
    Working mechanic with old particle system:



    Same mechanic but with new VFX as gameobject:




    This is the code for shooting the original gameobject along the raycast line:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor.VFX;
    5. using UnityEngine.Experimental.VFX;
    6.  
    7.  
    8. public class ShootLight : MonoBehaviour
    9. {
    10.     public float Length = 1000f;
    11.  
    12.     public Camera myCam;
    13.  
    14.  
    15.  
    16.     public GameObject blast;
    17.     public Ray ray;
    18.     public float lifetime;
    19.  
    20.     public GameObject SpawnPoint;
    21.  
    22.     public int MaxAmmo = 2;
    23.     private int currentAmmo;
    24.     public float cooldown = 2f;
    25.     private bool isReloading;
    26.  
    27.     private void Start()
    28.     {
    29.         currentAmmo = MaxAmmo;
    30.      
    31.     }
    32.  
    33.     void Update()
    34.     {
    35.         if(isReloading)
    36.         {
    37.             return;
    38.         }
    39.  
    40.         if(currentAmmo <= 0)
    41.         {
    42.             StartCoroutine(Reload());
    43.             return;
    44.         }
    45.      
    46.         ray = myCam.ScreenPointToRay(Input.mousePosition);
    47.  
    48.         Debug.DrawRay(ray.origin, ray.direction * Length, Color.red);
    49.  
    50.             if (Input.GetButtonDown("Fire1"))
    51.             {
    52.             Shoot();
    53.             }
    54.      
    55.     }
    56.  
    57.     IEnumerator Reload()
    58.     {
    59.         isReloading = true;
    60.         Debug.Log("Reloading");
    61.  
    62.         yield return new WaitForSeconds(cooldown);
    63.  
    64.         currentAmmo = MaxAmmo;
    65.         isReloading = false;
    66.  
    67.     }
    68.  
    69.     void Shoot()
    70.     {
    71.         currentAmmo--;
    72.  
    73.      
    74.         RaycastHit hit;
    75.  
    76.         Physics.Raycast(ray, out hit);
    77.  
    78.         GameObject clone = Instantiate(blast, SpawnPoint.transform.position, Quaternion.LookRotation(ray.direction));
    79.  
    80.         Destroy(clone, lifetime);
    81.  
    82.         //hit.rigidbody.AddForceAtPosition(ray.direction * hitForce, hit.point);
    83.         //Debug.Log("Hit");
    84.     }
    85.  
    86.  
    87.  
    88. }
     
  4. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    second particle system much more complex to follow path than first. but essentially if u parent particle to empty with property never dies or dies after x - second, particle should follow path of empty.
     
    Last edited: Feb 20, 2020
  5. Rhys_056

    Rhys_056

    Joined:
    Mar 27, 2019
    Posts:
    5
    I tried placing the particle as a child to an empty gameobject and deactivating the destroy function but unfortunately it still just spawns in-front of the player.
     
  6. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    what happen if you shoot bullet instead of raycast and parent particle to bullet?
     
  7. Rhys_056

    Rhys_056

    Joined:
    Mar 27, 2019
    Posts:
    5
    Yeah, it works without the raycast :mad: need to find a way for it to work with it. Thank you for your help though! Much appreciated
     
  8. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    u almost there am sure you will figure it out.
     
  9. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    did u fix it?