Entities show in the entity debugger, but won’t show in the game.
public struct GunData : IComponentData
{
// public float GunPivotX;
// public float GunPivotY;
public Entity Prefab;
// public float secondsBetweenSpawns;
// public float secondsToNextSpawn;
}
public class PlayerAuthoring : MonoBehaviour, IDeclareReferencedPrefabs, IConvertGameObjectToEntity
{
public Entity playerEntity;
public Transform GunPivot;
public GameObject Bullet;
private EntityManager em;
[SerializeField] private float spawnRate;
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
{
em = dstManager;
em.AddComponentData(entity, new PlayerInputData { Move = new float2(0, 0), Shoot = 0 });
em.AddComponentData(entity, new PlayerData { IsJumping = false });
em.AddComponentData(entity, new GunData
{
Prefab = conversionSystem.GetPrimaryEntity(Bullet),
// GunPivotX = GunPivot.position.x,
// GunPivotY = GunPivot.position.y,
// secondsBetweenSpawns = 1/spawnRate,
// secondsBetweenSpawns = 1,
// secondsToNextSpawn = 0f
});
playerEntity = entity;
}
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.name == "Floor")
{
em.SetComponentData(playerEntity, new PlayerData { IsJumping = false });
}
}
public void DeclareReferencedPrefabs(List<GameObject> referencedPrefabs)
{
referencedPrefabs.Add(Bullet);
}
}
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;
using UnityEngine.EventSystems;
public class PlayerShootingSystem : JobComponentSystem
{
private EndInitializationEntityCommandBufferSystem _bufferSystem;
protected override void OnCreate()
{
_bufferSystem = World.GetOrCreateSystem<EndInitializationEntityCommandBufferSystem>();
}
private struct PlayerShootingJob : IJobForEachWithEntity<GunData, PlayerInputData, LocalToWorld>
{
private EntityCommandBuffer.Concurrent _concurrent;
private readonly float _deltaTime;
public PlayerShootingJob(EntityCommandBuffer.Concurrent concurrent, float deltaTime)
{
_concurrent = concurrent;
_deltaTime = deltaTime;
}
public void Execute(Entity entity, int index, ref GunData gunData, ref PlayerInputData playerInputData, ref LocalToWorld localToWorld)
{
if (playerInputData.Shoot > 0)
{
Debug.Log("taco");
gunData.secondsToNextSpawn -= _deltaTime;
Entity instance = _concurrent.Instantiate(index, gunData.Prefab);
_concurrent.SetComponent(index, instance, new Translation
{
Value = localToWorld.Position
});
}
}
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
var playerShootingJob = new PlayerShootingJob(
_bufferSystem.CreateCommandBuffer().ToConcurrent(),
Time.deltaTime
);
JobHandle jobHandle = playerShootingJob.Schedule(this, inputDeps);
_bufferSystem.AddJobHandleForProducer(jobHandle);
return jobHandle;
}
}
