#Player not taking damage from collision

1 messages · Page 1 of 1 (latest)

boreal ibex
#

For some reason this code isnt working

using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.NetCode;
using Unity.Physics;
using Unity.Physics.Systems;

[BurstCompile]
[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
[UpdateAfter(typeof(PhysicsSystemGroup))]
partial struct CollisionEventsSystem : ISystem
{
    internal ComponentDataHandles m_ComponentDataHandles;

    internal struct ComponentDataHandles
    {
        public ComponentLookup<CharacterHealth> characterHealthLookup;
        public ComponentLookup<PhysicsVelocity> PhysicsVelocityLookup;
        public ComponentLookup<BulletData> bulletDataLookup;

        public ComponentDataHandles(ref SystemState systemState)
        {
            characterHealthLookup = systemState.GetComponentLookup<CharacterHealth>(false);
            PhysicsVelocityLookup = systemState.GetComponentLookup<PhysicsVelocity>(false);
            bulletDataLookup = systemState.GetComponentLookup<BulletData>(false);
        }

        public void Update(ref SystemState systemState)
        {
            characterHealthLookup.Update(ref systemState);
            PhysicsVelocityLookup.Update(ref systemState);
            bulletDataLookup.Update(ref systemState);
        }
    }

    [BurstCompile]
    public void OnCreate(ref SystemState state)
    {
        state.RequireForUpdate(state.GetEntityQuery(ComponentType.ReadOnly<PhysicsVelocity>()));
        m_ComponentDataHandles = new ComponentDataHandles(ref state);
    }

    public void OnUpdate(ref SystemState state)
    {
        m_ComponentDataHandles.Update(ref state);
        state.Dependency = new ProcessCollisionEventsJob
        {
            characterHealthLookup = m_ComponentDataHandles.characterHealthLookup,
            physicsVelocityLookup = m_ComponentDataHandles.PhysicsVelocityLookup,
            bulletDataLookup = m_ComponentDataHandles.bulletDataLookup,
#
        }.Schedule(SystemAPI.GetSingleton<SimulationSingleton>(), state.Dependency);
    }

    public struct ProcessCollisionEventsJob : ICollisionEventsJob
    {
        public ComponentLookup<CharacterHealth> characterHealthLookup;
        public ComponentLookup<BulletData> bulletDataLookup;
        public ComponentLookup<PhysicsVelocity> physicsVelocityLookup;
        public void Execute(CollisionEvent collisionEvent)
        {
            CheckEntityDamage(collisionEvent.EntityA, collisionEvent.EntityB);
            CheckEntityDamage(collisionEvent.EntityB, collisionEvent.EntityA);
        }

        public void CheckEntityDamage(Entity entA, Entity entB)
        {
            if (characterHealthLookup.HasComponent(entA) && bulletDataLookup.HasComponent(entB))
            {
                CharacterHealth entACharHealth = characterHealthLookup[entA];
                entACharHealth.currentHealth -= 1;
            }
        }
    }
}
#

ive checked and the player has the characterhealth component and the bullet has bullet data component, yet nothing is happening

desert rain
#

have you enabled collision events on your player? (on the rigidbody)

#

they don't get raised by default

boreal ibex
desert rain
#

(random side note, you forgot [BurstCompile])
have you chucked a log in your job to see what is actually being Executed()

#

oh hang on

#
CharacterHealth entACharHealth = characterHealthLookup[entA];
entACharHealth.currentHealth -= 1;```
#

this code does nothing

#

you're only editing the local value of CharacterHealth

#

you have to get it by refrw or write it back to the lookup

boreal ibex
desert rain
#

yes

boreal ibex
#

yep that worked thanks so much