#Bullet move to 0,0,0 after spawning?

1 messages · Page 1 of 1 (latest)

lament quiver
#

I'm unsure what I'm doing wrong here but all of my bullets are spawning at the right place, but then move to world 0,0,0 before moving in the right direction outwards towards the enemies they are supposed to hit.

I'll post some more comments with code.

The behaviour I want is that the bullet starts moving from the cannon that fired it, not the middle of the world.

#

The bullet authoring

namespace Game.Prototype
{
    public class BulletAuthoring : MonoBehaviour
    {
        public float Speed;
        public int Damage;

        class Baker : Baker<BulletAuthoring>
        {
            public override void Bake(BulletAuthoring authoring)
            {
                var entity = GetEntity(TransformUsageFlags.Dynamic);
                AddComponent(entity, new Bullet
                {
                    Direction = float3.zero,
                    Damage = authoring.Damage,
                    Speed = authoring.Speed
                });
            }
        }
    }

    public struct Bullet : IComponentData
    {
        public float3 Direction;
        public float Speed;
        public int Damage;
    }
}
#

Bullet Spawning system:

using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;

namespace Game.Prototype
{
    [UpdateAfter(typeof(CannonRotationSystem))]
    public partial struct CannonShootingSystem : ISystem
    {
        [BurstCompile]
        public readonly void OnCreate(ref SystemState state)
        {
            state.RequireForUpdate<Cannon>();
        }

        [BurstCompile]
        public readonly void OnUpdate(ref SystemState state)
        {
            // Update all cooldowns
            float deltaTime = SystemAPI.Time.DeltaTime;
            foreach (var cooldown in SystemAPI.Query<RefRW<Cooldown>>())
            {
                if (cooldown.ValueRW.Timer == 0) continue;
                cooldown.ValueRW.Timer = math.max(0, cooldown.ValueRW.Timer - deltaTime);
            }
#
            // Evaluate whether to spawn bullets or not
            foreach (var (cannon, cooldown) in
                SystemAPI.Query<RefRO<Cannon>, RefRW<Cooldown>>().WithAll<Shooting>())
            {
                if (cooldown.ValueRW.Timer > 0) continue;
                // reset timer
                cooldown.ValueRW.Timer = cooldown.ValueRW.Interval;
                // instantiate bullet
                var bulletEntity = state.EntityManager.Instantiate(cannon.ValueRO.BulletPrefab);
                // get bullet component
                var bulletEntityComp = state.EntityManager.GetComponentData<Bullet>(bulletEntity);
                // get bullet spawn point from cannon
                var bulletSpawn = state.EntityManager.GetComponentData<LocalToWorld>(cannon.ValueRO.BulletSpawn);
                // set bullet spawn point in world space
                state.EntityManager.SetComponentData(bulletEntity, bulletSpawn);
                // TODO Consider splitting up this component so that I only have to transform one value instead of 3
                // set bullet direction, copy over existing bullet properties
                state.EntityManager.SetComponentData(bulletEntity, new Bullet
                {
                    Direction = bulletSpawn.Forward,
                    Speed = bulletEntityComp.Speed,
                    Damage = bulletEntityComp.Damage
                });
            }
        }
    }
}
#

And lastly the Bullet Movement System

using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;

namespace Game.Prototype
{
    public partial struct BulletMovementSystem : ISystem
    {
        [BurstCompile]
        public readonly void OnCreate(ref SystemState state)
        {
            state.RequireForUpdate<Bullet>();
        }

        [BurstCompile]
        public void OnUpdate(ref SystemState state)
        {
            var bulletMovementJob = new BulletMovementJob
            {
                DeltaTime = SystemAPI.Time.DeltaTime
            };
            bulletMovementJob.Schedule();
        }
    }

    [BurstCompile]
    public partial struct BulletMovementJob : IJobEntity
    {
        public float DeltaTime;
        public void Execute(in Bullet bullet, ref LocalTransform transform)
        {
            float3 position = transform.Position + bullet.Direction * bullet.Speed * DeltaTime;

            transform = new LocalTransform
            {
                Position = position,
                Rotation = transform.Rotation,
                Scale = transform.Scale
            };
        }
    }
}
hot basalt
#

Are you forgetting to set the LocalTransform?

state.EntityManager.SetComponentData<LocalTransform>(new() { Position = _myBulletPosition });

lament quiver
#

Well, the LocalTransform is ref so wouldn't my bullet just stand still if I didn't?

scenic violet
#

You aren't setting LocalTransform on instantiation, you're only setting LTW
and LTW is overridden by LT value

lament quiver
#

I see. My problem here is that I need to spawn the bullet using a nested transform as origin, so I need to set the position using World coordinates. I wouldn't have a default LocalTransform to set I would imagine.

#

And I can see on the first frame where a bullet is spawned, it's spawned at the correct position, but then the next frame is moved.

#

Is that due to a default LocalTransform being set the next frame?

scenic violet
#

yes

#

transform system runs before end simulation

lament quiver
#

Ah

scenic violet
#

just set localtransform from the LTW you're setting

lament quiver
#

But how do I use one to set the other?
A LocalToWorld is just a 4x4 Matrix.

#

Are the values in c0-c3 just position, rotation, scale?

hot basalt
#

Isn't there something like LocalToWorld.Position?

lament quiver
#

No

#

There is LocalToWorld.Value

scenic violet
#

there is right there

#

are you using a really old version of entities?

#

0.51?

lament quiver
#

Huh

#

I guess Visual Studio might have been playing tricks on me then

#

It only showed me the Value

#

Yeah that did the trick. Thanks @hot basalt and @scenic violet
I would have done that had Visual Studio let me know I could. It's acting up really weirdly lately.

scenic violet
#

cough try rider cough

lament quiver
#

I might. I've been wanting to check it out for a while.
I have used IntelliJ Ultimate a long time ago for Android development and it was...rough. (Android like..version 2 or 3?)
I hear Rider improved a lot since.

scenic violet
#

can't say, i've never used jetbrains stuff outside of rider (and resharper)

lament quiver
#

Rider is just a different version of IntelliJ