Been working on a game that I’ve had in the last few Feedback Fridays over in Game Design. On my dev PC the game works perfectly fine, as seen in the GIF in this post . However on other machines it’s jerky and more importantly the bullet system, that uses trigger events, straight up isn’t working, as seen in this post . The bullets do move, which is done in OnUpdate, but the check for collisions doesn’t seem to be happening. The bullets should disappear when they hit enemies and walls.
The system is below.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Jobs;
using UnityEngine.Jobs;
using Unity.Entities;
using Unity.Transforms;
using System.Linq;
using Unity.Mathematics;
using Unity.Physics;
using Unity.Physics.Systems;
using Unity.Collections;
using Unity.Rendering;
using System.Threading.Tasks;
using Unity.Burst;
public class WeaponSystem:SystemBase
{
EntityCommandBufferSystem m_Barrier;
BuildPhysicsWorld physicsWorldSystem;
StepPhysicsWorld m_StepPhysicsWorldSystem;
EndFramePhysicsSystem EndFramePhysicsSystem;
protected override void OnCreate()
{
m_Barrier = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
physicsWorldSystem = Unity.Entities.World.DefaultGameObjectInjectionWorld.GetOrCreateSystem<BuildPhysicsWorld>();
m_StepPhysicsWorldSystem = World.DefaultGameObjectInjectionWorld.GetOrCreateSystem<StepPhysicsWorld>();
EndFramePhysicsSystem = World.DefaultGameObjectInjectionWorld.GetOrCreateSystem<EndFramePhysicsSystem>();
}
struct TriggerEventJob : ITriggerEventsJob
{
public EntityCommandBuffer.ParallelWriter cBuffer;
[NativeDisableParallelForRestriction] public ComponentDataFromEntity<WeaponEntity> weaponEntityInfo;
[NativeDisableParallelForRestriction] public ComponentDataFromEntity<PlayerEntity> playerEntityInfo;
[NativeDisableParallelForRestriction] public ComponentDataFromEntity<EnemyEntity> enemyEntityInfo;
[NativeDisableParallelForRestriction] public ComponentDataFromEntity<EnemyCageEntity> enemyStructureEntityInfo;
public void Execute(TriggerEvent collisionEvent)
{
CheckWeapon(collisionEvent.EntityA);
CheckWeapon(collisionEvent.EntityB);
CheckCollided(collisionEvent.EntityA, collisionEvent.EntityB);
CheckCollided(collisionEvent.EntityB, collisionEvent.EntityA);
}
void CheckWeapon(Entity entity)
{
if (weaponEntityInfo.HasComponent(entity))
{
var cur = weaponEntityInfo[entity];
if(cur.type != WeaponType.TrapBio && cur.type != WeaponType.TrapEn)
{
StaticClass.Damage(entity);
}
else
{
cur.stop = true;
cBuffer.SetComponent(entity.Index, entity, cur);
// still stop it, stick where it's at
}
}
}
void CheckCollided(Entity a, Entity b)
{
if(enemyStructureEntityInfo.HasComponent(a) && weaponEntityInfo.HasComponent(b))
{
var cur = weaponEntityInfo[b];
StaticClass.damageEnemyStructure?.Invoke(a, cur.type);
}
}
}
protected override void OnUpdate()
{
var commandBuffer = m_Barrier.CreateCommandBuffer().AsParallelWriter();
var weaponEntityInfo = GetComponentDataFromEntity<WeaponEntity>();
var playerEntityInfo = GetComponentDataFromEntity<PlayerEntity>();
var enemyEntityInfo = GetComponentDataFromEntity<EnemyEntity>();
var enemyStructureEntityInfo = GetComponentDataFromEntity<EnemyCageEntity>();
var deltaTime = Time.DeltaTime;
var physicsWorld = physicsWorldSystem.PhysicsWorld;
var collisionWorld = physicsWorld.CollisionWorld;
var grid = StaticClass.GetFlatWalkableGrid();
var gridHeight = StaticClass.interactiveGrids[StaticClass.currentLevel].GetLength(1);
var gridLength = grid.Length;
var cellWalkable0 = new NativeArray<CellWalkableState>(grid, Allocator.TempJob);
var cellWalkable = cellWalkable0.AsReadOnly();
var mapCorner = StaticClass.mapCorners[0];
var exploder = new NativeQueue<Entity>(Allocator.TempJob);
var exploderParallel = exploder.AsParallelWriter();
Entities
.WithName("WeaponSystem")
.WithBurst(Unity.Burst.FloatMode.Default, Unity.Burst.FloatPrecision.Standard, true)
.ForEach((Entity entity, int nativeThreadIndex, /*in Translation translation,*/ ref WeaponEntity weaponEntity, ref PhysicsVelocity velocity, ref Translation translation/*, in LocalToWorld localToWorld, in WorldRenderBounds bounds, in PhysicsCollider collider*/) =>
{
bool destroy = false;
if(weaponEntity.type == WeaponType.TrapBio || weaponEntity.type == WeaponType.TrapEn)
{
weaponEntity.countdown += deltaTime;
if(weaponEntity.stop)
{
velocity.Linear = new float3();
}
else
{
if (velocity.Linear.x > 0 || velocity.Linear.z > 0)
{
velocity.Linear.y = math.pow(weaponEntity.countdown + 0.5f, 4) * -10;
}
if (translation.Value.y <= 0)
{
velocity.Linear = new float3(0, 0, 0);
translation.Value.y = 0;
}
}
}
if(weaponEntity.countdown > 1)
{
exploderParallel.Enqueue(entity);
}
if (destroy)
{
commandBuffer.DestroyEntity(nativeThreadIndex, entity);
}
})
.ScheduleParallel();
Dependency = new TriggerEventJob {
cBuffer = commandBuffer,
weaponEntityInfo = weaponEntityInfo,
playerEntityInfo = playerEntityInfo,
enemyEntityInfo = enemyEntityInfo,
enemyStructureEntityInfo = enemyStructureEntityInfo,
}
.Schedule(m_StepPhysicsWorldSystem.Simulation, ref physicsWorldSystem.PhysicsWorld, Dependency);
m_Barrier.AddJobHandleForProducer(Dependency);
Dependency.Complete();
for (int i = 0; i < exploder.Count; i++)
{
StaticClass.Damage(exploder.Dequeue());
}
cellWalkable0.Dispose();
exploder.Dispose();
}
}
Can anyone point out what might be causing this to work on my dev machine but literally no others?