Currently I have a “Player” prefab which has a “Bullet Spawn” prefab as a child.
Player (which has a PlayerTag authoring component)
Bullet Spawn (which has a BulletSpawnTag authoring component)
When I spawn a bullet what I currently do is grab the buffer of children entities from the Player entity, cycle through the children and find the child entity that is the Bullet Spawn through its BulletSpawnTag. Then I find its current localToWorld position and spawn the bullet there.
var childFromEntity = GetBufferFromEntity(true);
Entities
.WithAll<PlayerTag, Child>() (I query the player entities)
.ForEach(Entity entity, ref Translation position…)
var childBuffer = childFromEntity[entity] (I grab the children buffer)
foreach(Entity child in childBuffer)
If (HasComponent) (I grab the bullet spawn child entity)
var bulletSpawnPosition = localToWorldFromEntity[children*.Value].Position;* var newPosition = new Translation {Value = bulletSpawnPosition}; I spawn the bullet at the position of the Bullet Spawn localToWorld Is there a way to spawn an entity by saying “spawn this entity at bulletSpawnOffset localToParent position”? Or in other words, can I somehow spawn entities from an offset of a position without calling localToWorld?
I have a NetCode project where I need to spawn these bullets.
Currently my bullets spawn “weird” like away from my player and then get back in line. I was thinking maybe the issue was the time it took to compute where the bullet should spawn causes this hiccup.
Why are you attaching bullets entities to player?
They should be completely independent.
The only time the bullet is related to player, is at spawn / shoot time, when you grab position and orientation of the player’s gun.
But you shouldn’t do that, by attaching bullets as player’s children.
What I meant to say was at that time you mentioned, “when you grab position and orientation of the player’s gun”, I current go through the child entities of the Player object and find the “Bullet Spawn” location.
I do that by using “LocalToWorld” of that entity.
I am wondering if there is a way I could instead say something along the lines of “spawn the bullet this amount of offset from the player prefab”
I am thinking now that I just do position - offsetfloat3 and call it a day.
Well, looking specifically through number of children, is not best approach, if can be avoided.
There is number of way you could do this.
But for example, you could have gun component on player, which contains references to gun entity.
So you directly can grab gun entity of player, without player’s children iteration.
Gun of course would be also a child of player, which allows to position in relative to player.
But you wouldn’t need to concern specifically about children anymore.
So I thought I could simply do position.Value + bulletOffset.Value
I realize that when I rotate, this breaks because If I turn around, the bullet is actually spawning behind me. I actually need to spawn the bullet (.5, 0, 1) relative to the parent.
You can also grab wolrd position of the gun itself, along with its orientation, to get bullet direction.
I would probably calculate forward vector for player, or gun and use that for both movement and spawning bullets, so I got fewer calculations of rotations per each bullet entity.
Let’s say parent parent has Transform position and Rotation rotation.
How do I take the parents position and rotation, and then offset it by (.5,0,1) (from where it is “facing”)? Is there a good “ECS” way to do this other than
position.Value + math.mul(rotation.Value, new float3(.5,0,1)).xyz)?