Dealing with targets

Targets are used by the aim-action to determine the rotation of aimed bullet-sources. You can statically assign targets to your DanmakuContexts but you can also register and unregister targets at runtime. This is useful for example, if you want to implement homing projectiles for your players which will most likely need a dynamic set of enemy targets that are currently visible.

You can also override the default behaviour of target determination to fit your individual needs.

Registering / unregistering targets

To register/unregister targets at runtime, simply pass a Transform to the methods RegisterTarget and UnregisterTarget of a DanmakuContext.

Example

using UnityEngine;
using VirtualDropkick.DanmakuEngine.Unity;

public class MyEnemy : MonoBehaviour
{
    private DanmakuContext context;
    
    void Start()
    {
        context = DanmakuController.Instance.GetContext("Player");

        // register this object's Transform as a target
        context.RegisterTarget(transform);
    }

    void OnDestroy()
    {
        // unregister when this object gets destroyed
        context.UnregisterTarget(transform);
    }
}

Custom target determination

If you want to implement your own solution for resolving targets, simply assign methods or delegates to the properties ClosestTargetResolver, FarthestTargetResolver and/or RandomTargetResolver of a DanmakuContext with the following interface:

Transform (DanmakuBulletSource bulletSource)

Example

using UnityEngine;
using VirtualDropkick.DanmakuEngine.Unity;

public class MyTargetResolver : MonoBehaviour
{
    private DanmakuContext context;
    
    public Transform[] myPlayers;

    void Awake()
    {
        context = DanmakuController.Instance.GetContext("Enemy");

        // register target resolver delegates
        context.ClosestTargetResolver = GetClosestTarget;
        context.FarthestTargetResolver = GetFarthestTarget;
        context.RandomTargetResolver = GetRandomTarget;
    }

    private Transform GetClosestTarget(DanmakuBulletSource bulletSource)
    {
        Transform target = null;

        float distance = 0f;
        for(int i=0; i < myPlayers.Length; i++)
        {
            float newDistance = Vector2.Distance(
                (Vector2)myPlayers[i].transform.position,
                bulletSource.Position
            );

            if(target == null || newDistance < distance)
            {
                target = myPlayers[i];
                distance = newDistance;
            }
        }

        return target;
    }

    private Transform GetFarthestTarget(DanmakuBulletSource bulletSource)
    {
        Transform target = null;
        
        float distance = 0f;
        for(int i=0; i < myPlayers.Length; i++)
        {
            float newDistance = Vector2.Distance(
                (Vector2)myPlayers[i].transform.position,
                bulletSource.Position
            );
                        
            if(target == null || newDistance > distance)
            {
                target = myPlayers[i];
                distance = newDistance;
            }
        }
        
        return target;
    }

    private Transform GetRandomTarget(DanmakuBulletSource bulletSource)
    {
        return myPlayers[Random.Range(0, myPlayers.Length)];
    }
}

 


Buy the Danmaku Engine plugin

Leave a comment