Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question How to interpolate/apply velocity?

Discussion in 'Physics for ECS' started by JeffTheCampbell, Jan 9, 2024.

  1. JeffTheCampbell

    JeffTheCampbell

    Joined:
    Jul 11, 2018
    Posts:
    35
    I'm working on building a simple top-down 2D shooter to better understand ECS in general (covered in more detail here) when I ran into this issue where my bullets seem to sometimes have their initial position/velocity delayed when created.

    Unity: 2022.3.16f1
    Unity Physics: 1.2.0-pre.6
    Unity Entities: 1.2.0-pre.6



    It appears to go away if I turn off Smoothing on the PhysicsBody for the projectiles, but then this introduces jitter (makes sense haha).



    The projectiles have been set to collide only with the enemy or projectiles and do not have any damping set.

    upload_2024-1-9_8-45-47.png

    This is my system for handling spawning projectiles from ships, creates and sets up the projectiles.

    Code (CSharp):
    1. using Unity.Burst;
    2. using Unity.Entities;
    3. using Unity.Mathematics;
    4. using Unity.Physics;
    5. using Unity.Transforms;
    6.  
    7. namespace Game.ECS
    8. {
    9.     /// <summary>
    10.     /// A system for handling projectile weapon fire from ships.
    11.     /// </summary>
    12.     public partial struct ProjectileFireWeaponSystem : ISystem
    13.     {
    14.         public void OnCreate(ref SystemState state)
    15.         {
    16.             state.RequireForUpdate<BeginSimulationEntityCommandBufferSystem.Singleton>();
    17.         }
    18.  
    19.         [BurstCompile]
    20.         public void OnUpdate(ref SystemState state)
    21.         {
    22.             var singleton = SystemAPI.GetSingleton<BeginSimulationEntityCommandBufferSystem.Singleton>();
    23.             var ecb = singleton.CreateCommandBuffer(state.WorldUnmanaged);
    24.             foreach (var (localTransformRO,
    25.                          inputFireRO,
    26.                          projectileWeaponRW,
    27.                          gunPoints) in
    28.                      SystemAPI.Query<
    29.                          RefRO<LocalTransform>,
    30.                          RefRO<InputFire>,
    31.                          RefRW<ProjectileWeapon>,
    32.                          DynamicBuffer<GunPoint>
    33.                      >().WithAll<Ship>())
    34.             {
    35.                 var canFire = inputFireRO.ValueRO.value && projectileWeaponRW.ValueRO.reloadTime <= 0;
    36.                 if (canFire)
    37.                 {
    38.                     if (projectileWeaponRW.ValueRO.projectile != Entity.Null)
    39.                     {
    40.                         // Create as many bullets as there are gunpoints on a given ship.
    41.                         for (var i = 0; i < gunPoints.Length; i++)
    42.                         {
    43.                             var bulletPrefab = projectileWeaponRW.ValueRO.projectile;
    44.  
    45.                             // Get the needed components for the bullet instance and gunpoint
    46.                             var gptRO = SystemAPI.GetComponentRO<LocalTransform>(gunPoints[i].entity);
    47.                             var physicsColliderRO =
    48.                                 SystemAPI.GetComponentRO<PhysicsCollider>(bulletPrefab);
    49.                             var bulletVelocityRW =
    50.                                 SystemAPI.GetComponentRW<PhysicsVelocity>(bulletPrefab);
    51.  
    52.                             // Calculate the world pos and rotation of the bullet entity.
    53.                             // Determine height-offset for the bullet from the gunpoint position.
    54.                             float bulletHeight;
    55.                             unsafe
    56.                             {
    57.                                 var boxColliderPtr = (BoxCollider*)physicsColliderRO.ValueRO.ColliderPtr;
    58.                                 bulletHeight = boxColliderPtr->Size.y;
    59.                             }
    60.  
    61.                             var spawnOffsetPos = gptRO.ValueRO.Position + new float3(0, bulletHeight / 2f, 0);
    62.                             var gptPos = localTransformRO.ValueRO.TransformPoint(spawnOffsetPos);
    63.                             var gptRot = localTransformRO.ValueRO.TransformRotation(gptRO.ValueRO.Rotation);
    64.  
    65.                             // Calculate the velocity of the bullets
    66.                             var gptForward = localTransformRO.ValueRO.TransformDirection(gptRO.ValueRO.Up());
    67.                             var linearVelocity = gptForward * projectileWeaponRW.ValueRO.speed;
    68.                             bulletVelocityRW.ValueRW.Linear = linearVelocity;
    69.  
    70.                             // Apply the final position, rotation, and velocity of the bullet
    71.                             var bulletEntity = ecb.Instantiate(projectileWeaponRW.ValueRO.projectile);
    72.                             ecb.AddComponent(bulletEntity, new LocalTransform
    73.                             {
    74.                                 Position = gptPos,
    75.                                 Rotation = gptRot,
    76.                                 Scale = 1
    77.                             });
    78.                             ecb.AddComponent(bulletEntity, bulletVelocityRW.ValueRO);
    79.                         }
    80.                     }
    81.  
    82.                     projectileWeaponRW.ValueRW.reloadTime = projectileWeaponRW.ValueRO.maxReloadTime;
    83.                 }
    84.                 else
    85.                 {
    86.                     projectileWeaponRW.ValueRW.reloadTime = math.max(
    87.                         0f,
    88.                         projectileWeaponRW.ValueRO.reloadTime - SystemAPI.Time.DeltaTime);
    89.                 }
    90.             }
    91.         }
    92.     }
    93. }
    94.  
    My questions are:
    • Is there a different way I should be applying velocity or movement changes to physics objects in Dots?
    • Are there any system dependencies I am not accounting for with this system?
    • Are there alternate approaches I should be thinking about with Unity Physics for this type of use case?
     

    Attached Files:

    Neiist and bb8_1 like this.