Advertisement
newbninja

Shooting Improvements - Bullets Dont Move?

Dec 16th, 2024
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.17 KB | None | 0 0
  1. using Unity.Burst;
  2. using Unity.Entities;
  3. using Unity.Transforms;
  4. using Unity.Mathematics;
  5.  
  6. partial struct BulletMoverSystem : ISystem
  7. {
  8.     [BurstCompile]
  9.     public void OnUpdate(ref SystemState state)
  10.     {
  11.         EntityCommandBuffer entityCommandBuffer =
  12.             SystemAPI.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>().CreateCommandBuffer(state.WorldUnmanaged);
  13.  
  14.         foreach ((
  15.             RefRW<LocalTransform> localTransform,
  16.             RefRO<Bullet> bullet,
  17.             RefRO<Target> target,
  18.             Entity entity)
  19.             in SystemAPI.Query<
  20.                 RefRW<LocalTransform>,
  21.                 RefRO<Bullet>,
  22.                 RefRO<Target>>().WithEntityAccess()) {
  23.  
  24.  
  25.             // If the target no longer exists, destroy the bullet
  26.             /// 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
  27.             if (target.ValueRO.targetEntity == Entity.Null) {
  28.                 entityCommandBuffer.DestroyEntity(entity);
  29.                 continue;
  30.             }
  31.  
  32.             // Shoot towards the target
  33.             LocalTransform targetLocalTransform = SystemAPI.GetComponent<LocalTransform>(target.ValueRO.targetEntity);  // Get target local position (unit position)
  34.             ShootVictim targetShootVictim = SystemAPI.GetComponent<ShootVictim>(target.ValueRO.targetEntity);           // Get target hit offset (chest)
  35.             float3 targetPosition = targetLocalTransform.TransformPoint(targetShootVictim.hitLocalPosition);            // Set our bullets destination (chest)
  36.  
  37.             float distanceBeforeSq = math.distancesq(localTransform.ValueRO.Position, targetPosition);            // Overshot check (v0.1.19)
  38.  
  39.             float3 moveDirection = targetPosition - localTransform.ValueRO.Position;
  40.             moveDirection = math.normalize(moveDirection);
  41.             localTransform.ValueRW.Position += moveDirection * bullet.ValueRO.speed * SystemAPI.Time.DeltaTime;
  42.  
  43.             float distanceAfterSq = math.distancesq(localTransform.ValueRO.Position, targetPosition);            // Overshot check (v0.1.19)
  44.  
  45.             // Overshot the target check
  46.             /// 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)
  47.             if (distanceAfterSq > distanceBeforeSq) {
  48.                 // We've shot past the target - correct the current position so it gets destroyed correctly
  49.                 localTransform.ValueRW.Position = targetPosition;
  50.             }
  51.  
  52.             // Bullet to target - distance check
  53.             float destroyDistanceSq = .2f;
  54.  
  55.             if (math.distancesq(localTransform.ValueRO.Position, targetPosition) < destroyDistanceSq) {
  56.  
  57.                 // Close enough - Do damage to the target
  58.                 RefRW<Health> targetHealth = SystemAPI.GetComponentRW<Health>(target.ValueRO.targetEntity);
  59.                 targetHealth.ValueRW.healthAmount -= bullet.ValueRO.damageAmount;
  60.  
  61.                 // Destroy the bullet
  62.                 entityCommandBuffer.DestroyEntity(entity);
  63.             }
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement