Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Unity.Burst;
- using Unity.Entities;
- using Unity.Transforms;
- using Unity.Mathematics;
- partial struct BulletMoverSystem : ISystem
- {
- [BurstCompile]
- public void OnUpdate(ref SystemState state)
- {
- EntityCommandBuffer entityCommandBuffer =
- SystemAPI.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>().CreateCommandBuffer(state.WorldUnmanaged);
- foreach ((
- RefRW<LocalTransform> localTransform,
- RefRO<Bullet> bullet,
- RefRO<Target> target,
- Entity entity)
- in SystemAPI.Query<
- RefRW<LocalTransform>,
- RefRO<Bullet>,
- RefRO<Target>>().WithEntityAccess()) {
- // If the target no longer exists, destroy the bullet
- /// This is DIRTY and really, we should just allow the bullet to move offscreen at its curent velocity and then remove it but it'll do for now
- if (target.ValueRO.targetEntity == Entity.Null) {
- entityCommandBuffer.DestroyEntity(entity);
- continue;
- }
- // Shoot towards the target
- LocalTransform targetLocalTransform = SystemAPI.GetComponent<LocalTransform>(target.ValueRO.targetEntity); // Get target local position (unit position)
- ShootVictim targetShootVictim = SystemAPI.GetComponent<ShootVictim>(target.ValueRO.targetEntity); // Get target hit offset (chest)
- float3 targetPosition = targetLocalTransform.TransformPoint(targetShootVictim.hitLocalPosition); // Set our bullets destination (chest)
- float distanceBeforeSq = math.distancesq(localTransform.ValueRO.Position, targetPosition); // Overshot check (v0.1.19)
- float3 moveDirection = targetPosition - localTransform.ValueRO.Position;
- moveDirection = math.normalize(moveDirection);
- localTransform.ValueRW.Position += moveDirection * bullet.ValueRO.speed * SystemAPI.Time.DeltaTime;
- float distanceAfterSq = math.distancesq(localTransform.ValueRO.Position, targetPosition); // Overshot check (v0.1.19)
- // Overshot the target check
- /// BUG FIX: To prevent high speed projectile from overshooting the target and rattling back n forth until it eventually hit's the target (v0.1.19)
- if (distanceAfterSq > distanceBeforeSq) {
- // We've shot past the target - correct the current position so it gets destroyed correctly
- localTransform.ValueRW.Position = targetPosition;
- }
- // Bullet to target - distance check
- float destroyDistanceSq = .2f;
- if (math.distancesq(localTransform.ValueRO.Position, targetPosition) < destroyDistanceSq) {
- // Close enough - Do damage to the target
- RefRW<Health> targetHealth = SystemAPI.GetComponentRW<Health>(target.ValueRO.targetEntity);
- targetHealth.ValueRW.healthAmount -= bullet.ValueRO.damageAmount;
- // Destroy the bullet
- entityCommandBuffer.DestroyEntity(entity);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement