Unity3D cool special effects tracking arc

overview

This article is developed based on the arc made by the tutorial ([[Unity Tutorial Handling] Unity VFX Graph - Arc Tutorial] https://www.bilibili.com/video/BV1hm4y1r7H6?vd_source=033061353294ae0352cec4c059f20a65) , friends can watch the video first After completing the material preparation, of course, you can also buy it directly through the unity store (I didn’t say that Taobao can also buy it), material name: VFX Graph - Procedural Electricity - Vol. 1.

The effect is displayed first

This is the effect picture, using the dynamic binding method based on VFX Property Binder, and wrote a new property PosBinder inheriting VFXBinderBase

thought process

As mentioned in the tutorial mentioned in red above, this arc can dynamically bind several coordinates according to the VFXPropertyBinder component it carries, that is, drag the component with transform to the binding point Postion on the Inspect interface of the arc. The target is enough, as shown in the figure.

In the following, we will refer to these things with green squares as VFX Property (because I am not sure whether this thing is named like this, and I have not studied it very thoroughly), as shown in the figure below

 

 (Continued from the black text above) But this leads to a serious problem. If the binding is not performed in advance, the current cannot actively bind the enemy's information to Poston.target when the game is running, and the VFX Property data provided by Unity itself It is packaged in private. Although the serialized (SerializeField) information can be seen on the interface, it cannot be obtained by code, so relying on the original VFX Property, we cannot perform dynamic binding at runtime. At this time We need to write a new VFX Property to provide public data for us to dynamically bind at runtime.

After checking the documentation of VFXPropertyBinder, we learned that these VFX Properties are implemented by inheriting a thing called VFXBinderBase, as shown in the figure below

 So we customize a new class called PosBinder (this is whatever you like, it doesn't matter the name), which inherits VFXBinderBase. Then implement the functions required by VFXBinderBase.

Theory works, enter practice

 It is written as follows:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.VFX;
using UnityEngine.VFX.Utility;

[ExecuteInEditMode]
public class PosBinder : VFXBinderBase
{
    public ExposedProperty property;
    public Transform Target;

    public override bool IsValid(VisualEffect component)
    {
        return component.HasVector3(property);
    }

    public override string ToString()
    {
        return "Postion:"+ property.ToString()+"->"+ Target;
    }

    public override void UpdateBinding(VisualEffect component)
    {
        component.SetVector3(property, Target.position);
    }
}

ExposedProperty is to give us a name for the binding node,

Target is the target that our node is bound to.

The IsValid method is used to check whether the node has been registered repeatedly, etc.

ToString is the origin of the string of characters behind the green square

The core part is the UpdateBinding function. This function realizes the dynamic binding of nodes in the game. You must be familiar with Update. This function is responsible for binding the target of the node every frame to prevent it from derailing. , the specific implementation method will not be described in detail. 

After the implementation of PosBinder is completed, we need to add this PosBinder to the VFXPropertyBinder of the arc in the way of AddComponent in the Unity interface, as shown in the figure (this method is mainly used here because the blogger does not know how to write PosBinder into the plus sign in the lower right corner , if there is a big guy who has a way to achieve it, please give me more advice)

 After putting it up, PosBinder also needs to be bound by default to ensure that the game will not report an error when creating it, so we use it as the parent object, just create a few empty objects and put it on, like this

 Now the arc part is finished, we need a carrier to call its properties and use it to dynamically bind with the enemy, just anything, the implementation code is as follows

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEngine.VFX;
using UnityEngine.VFX.Utility;

[RequireComponent(typeof(Collider),typeof(Rigidbody),typeof(Transform))]
public class Bullet : MonoBehaviour
{
    private VFXPropertyBinder[] binder;
    private VisualEffect[] visualEffect;
    //bullet property difine
    public Collider BltColider;
    public Rigidbody BltRigidbody;
    public GameObject lightning;
    public List<GameObject> lightnings = new List<GameObject>();
    public int forwardForce=200;
    // Start is called before the first frame update
    void Start()
    {
        
        BltColider = gameObject.GetComponent<Collider>();
        BltRigidbody = gameObject.GetComponent<Rigidbody>();
        GetComponent<Rigidbody>().AddForce(transform.forward * forwardForce);
    }

    // Update is called once per frame
    void Update()
    {
        
        
    }

   
    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "enemy")
        {
            
           // Position v1;
           

            Vector3 direction = other.transform.position - transform.position;
            GameObject lightningAttack = Instantiate(lightning, transform.position, transform.rotation);
            lightnings.Add(lightningAttack);
            binder = lightningAttack.GetComponentsInChildren<VFXPropertyBinder>();
            visualEffect = lightningAttack.GetComponentsInChildren<VisualEffect>();
            
                PosBinder[] posBinders = lightningAttack.GetComponentsInChildren<PosBinder>();
                posBinders[0].Target = transform;
                posBinders[1].Target = transform;
                posBinders[2].Target = transform;
                posBinders[3].Target = other.transform;
                posBinders[4].Target = transform;
                posBinders[5].Target = transform;
                posBinders[6].Target = transform;
                posBinders[7].Target = other.transform;
 
        }
    }
    
    private void OnDestroy()
    {
        for(int i = 0; i < lightnings.Count; i++)
        {
            Destroy(lightnings[i]);
        }
    }

}

Put this script on your arc bearer when you're done, it would be great to have electric balls. This carrier needs to have these components, and put the made arc on Lightning

 epilogue

Alright, now your electric arc has been dynamically bound to the enemy. I know you want to learn about explosions and electric balls. I will publish articles related to electric balls and explosions for you to read as soon as possible. If you like it, please return it. Please click a little like, thank you for being a good person, a super nice person.

Guess you like

Origin blog.csdn.net/qq_56755504/article/details/131272992