How can I consistently prioritize a certain physics layer to be evaluated before other layers?
Example:
An arrow flies toward a wall, it should damage the wall and then get destroyed because it met an environment.
I have a an arrow that destroys itself when it meets an “Environment” (layer #1) and I have a hurtbox script that allows the wall to lose hp “Hurtbox” (layer #2).
I would like for my arrows to always trigger the Hurtbox layer first so that the arrow doesn’t get destroyed before dealing damage to the wall.
After severals tests, the arrow gets destroyed before dealing damage half of the time.
I expected something like this would work but for some reason I still get the previously mentioned results.
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
/// <summary>
/// The purpose of this class is to get any Trigger events that occured during this frame.
/// Once that frame is over, it will determine in which order the events should be processed.
/// </summary>
public class PhysicsPrioritizer : MonoBehaviour
{
public class PhysicsEvent
{
public PhysicsEvent(Collider2D _other, Action<Collider2D> _event)
{
Other = _other;
Event = _event;
}
public Collider2D Other;
public Action<Collider2D> Event;
}
static List<PhysicsEvent> HitboxEvents = new List<PhysicsEvent>();
static List<UnityEvent> UnityEvents = new List<UnityEvent>();
public static void PassHitboxEvent(Collider2D _other, Action<Collider2D> _event)
{
HitboxEvents.Add(new PhysicsEvent(_other, _event));
}
public static void PassPhysicsEvent(UnityEvent _event)
{
UnityEvents.Add(_event);
}
void LateUpdate()
{
// Hitboxes are to be processed first!
foreach (var e in HitboxEvents)
{
if (e.Other)
{
e.Event.Invoke(e.Other);
}
}
// Lastly, any unity events.
foreach (var e in UnityEvents)
{
e.Invoke();
}
// We processed everything now we clear the events
HitboxEvents.Clear();
UnityEvents.Clear();
}
}
just make wall loose hp before its destroyed on your trigger event