Hi All,
i googled for hours now and am still not sure where the main issue lies.
quick overview:
AS some people wantedstable Unity the 2018 LTS one was chosen (at least for no)
i wanted to optimize some stuff and used ECS, works mostly good for what i need it so far.
Prefabs are as Gameobjects. i added the components i need to them so peoplle can edit values to try stuff out. fully ECS is not an option at this time.
but now:
i wanted to let turrets spawn their bullets in a ComponentSystem. this worked until i saw that my MonoBehaviour collision detection
void OnTriggerEnter2D(Collider2D target)
{
“target” does not have the components in it’s entity i have added in the prefabs. it DOES have them in the GameObject tough.
funnily, if another ship collides they DO have them in the entity (looking for has component in entity manager)
i tried multiple things, including making a singleton manager that only has a queue of objects that the monobehaviour that for now spawns enemies (the ones that “work”) creates instances from.
My TurretSystem: (sorry stuff is a little messy now from trying)
using UnityEngine;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
public class TurretSystem : ComponentSystem
{
private ComponentGroup enemies;
private ComponentGroup players;
// Here we define the group
protected override void OnCreateManager()
{
// Get all entities with TurretComponent and Transform
enemies = GetComponentGroup(
typeof(TurretComponent),
typeof(Transform)
);
// Get all entities with PlayerComponent and Transform
players = GetComponentGroup(
typeof(PlayerComponent),
typeof(Transform)
);
}
protected override void OnUpdate()
{
Testmanager testmanager = Testmanager.Instance;
//Get Delta Time
var dt = Time.deltaTime;
// Get components array
var playerTransformInput = players.GetTransformAccessArray();
var turretInput = enemies.GetComponentArray<TurretComponent>();
//tranform is a special snowflake so special access array
var turretTransformInput = enemies.GetTransformAccessArray();
// Iterate over components. all entities are at the same position in all arrays so i will match it.
for (int i = 0; i < turretInput.Length; ++i)
{
TurretComponent turretComponent = turretInput[i];
Transform turretTransform = turretTransformInput[i];
//Setting back the timer
float cooldownTimer = turretInput[i].cooldownTimer - dt;
if (turretComponent.turretType == TurretComponent.TurretType.aimAtPlayer)
{
turretTransform = TransformHelper.AimAtPlayer(playerTransformInput, turretTransform, turretComponent.rotationSpeed, dt);
}
//aaaand copy paste from old system
if (cooldownTimer <= 0f)
{
turretInput[i].cooldownTimer = turretComponent.fireDelay;
Vector3 posOffset = turretTransform.rotation * new Vector3(0, 3f, 0);
Quaternion rot = turretTransform.rotation;
float z = rot.eulerAngles.z;
rot = Quaternion.Euler(0, 0, z);
//BulletWrapper bwr = new BulletWrapper(turretComponent.bulletType, turretTransform.position + posOffset, rot);
//testmanager.Bullets.Enqueue(bwr);
GameObject Bullet = GameObject.Instantiate(turretComponent.bulletType, turretTransform.position + posOffset, rot);
//Entity buen = GameObjectEntity.AddToEntityManager(EntityManager, Bullet);
//GameObjectEntity.CopyAllComponentsToEntity(Bullet, EntityManager, buen);
turretInput[i].cooldownTimer = turretComponent.fireDelay;
}
else
{
turretComponent.cooldownTimer = cooldownTimer;
}
}
}
}
and the on trigger check
void OnTriggerEnter2D(Collider2D target)
{
// Get entity ID from GameObjectEntity component attached to this GameObject.
var localEntity = this.GetComponent<GameObjectEntity>().Entity;
var localy = this.gameObject;
var targetEntity = target.GetComponent<GameObjectEntity>().Entity;
var targety = target.GetComponent<GameObjectEntity>();
if (targety.EntityManager == null)
{
GameObjectEntity.AddToEntityManager(entityManager, target.gameObject);
}
targety = target.GetComponent<GameObjectEntity>();
if (entityManager.HasComponent<CollisionComponent>(localEntity)){
CollisionComponent localColl = entityManager.GetComponentObject<CollisionComponent>(localEntity);
if (localColl != null)
{
localColl.isTarget = false;
localColl.isColliding = true;
}
if (entityManager.HasComponent<CollisionComponent>(targetEntity) )
{
CollisionComponent targetColl = entityManager.GetComponentObject<CollisionComponent>(targetEntity);
HitTypeComponent hitTypeComponent = entityManager.GetComponentObject<HitTypeComponent>(targetEntity);
if (targetColl != null)
{
//TODO:damage handler generalization should change this
localColl.hitType = hitTypeComponent.hitType;
if(hitTypeComponent.hitType == HitTypeComponent.HitType.Powerup && entityManager.HasComponent<PowerupComponent>(targetEntity))
{
PowerupComponent powerupComponent = entityManager.GetComponentObject<PowerupComponent>(targetEntity);
localColl.powerupType = powerupComponent.powerupType;
}
targetColl.isTarget = true;
targetColl.isColliding = true;
}
}
}