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. Dismiss Notice

Question Set the component data of VFX error.[Solved]

Discussion in 'Entity Component System' started by iamshenkui_gee, Jul 15, 2022.

  1. iamshenkui_gee

    iamshenkui_gee

    Joined:
    Apr 24, 2021
    Posts:
    30
    Hi, I am adding hitting VFX to my game. I use the event system to trigger the VFX. However, the VFX will always appear at the last hit position. And the first position of the VFX will be the translation of the prefab's translation. What can I do about this situation?
    here is my hit system to spawn the VFX.
    Code (CSharp):
    1. public partial class FireBallHitSystem : SystemBase
    2. {
    3.     EndSimulationEntityCommandBufferSystem ecbSystem;
    4.     private Entity hitPrefab;
    5.     NativeQueue<float2> positions;
    6.     EntityCommandBuffer ecb;
    7.     protected override void OnCreate()
    8.     {
    9.         ecbSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
    10.         World.GetOrCreateSystem<FireBallAttackSystem>().OnFireBallHit += EventSubscribe;
    11.     }
    12.  
    13.  
    14.     private void EventSubscribe(object sender, FireballHitEventArgs e) {
    15.         ecb.Instantiate(hitPrefab);
    16.         Debug.Log(e.x +"    ###   " + e.y);
    17.         ecb.SetComponent(hitPrefab, new Translation { Value = new float3 { x = e.x, y = e.y } });
    18.      
    19.     }
    20.  
    21.     protected override void OnUpdate()
    22.     {
    23.         if(hitPrefab == Entity.Null)
    24.         {
    25.             hitPrefab = GetSingleton<YellowHitAuthoringComponent>().Prefab;
    26.  
    27.             //we must "return" after setting this prefab because if we were to continue into the Job
    28.             //we would run into errors because the variable was JUST set (ECS funny business)
    29.             //comment out return and see the error
    30.             return;
    31.         }
    32.  
    33.  
    34.         ecb = ecbSystem.CreateCommandBuffer();
    35.  
    36.         //Entities.ForEach((VisualEffect visualEffect ) => {
    37.         //    visualEffect.Play();  
    38.         //})
    39.         //    .WithoutBurst()
    40.         //    .Run();
    41.     }
    42. }
     
    Last edited: Jul 15, 2022
  2. iamshenkui_gee

    iamshenkui_gee

    Joined:
    Apr 24, 2021
    Posts:
    30
    Well, I found the bug in my code, I forgot to use the new entity as the reference.