Skip to content

Adding a force to an entity

The Rigidbody component allows you to apply physics-based forces to entities and make them respond to gravity, collisions, and other physical properties. In this tutorial, you will learn how to:

  • Add a constant force to an entity.
  • Add an explosion force to an entity.

Add a constant force to an entity

To add a constant force to an entity with rigidbody:

  1. Add an entity to the scene and make sure the entity has a non-mesh collider attached to it.

    • In this tutorial, we use model Spikes_Normal to the scene by selecting it via Scenery>Models>Basics.

    • The model added comes with a mesh collider, so we turn off the mesh collider and add a sphere collider.

    Change_collider

  2. Add a rigidbody to the entity by choosing + Component>Gameplay>Rigidbody.

  3. Add the following script to the entity to apply a constant force to it. For more information on how to add a script, see Creating or adding scripts.

    local selfEntity = script:SelfEntity()
    
    -- Set a 0.05-second delay before starting and 0.1 seconds to wait between events 
    local timer = YaTime:ScheduleAtInterval(0.05, 0.1)
    
    -- Set a force of 3000 Newtons in a specific direction 
    local force = 3000
    local forwardForce = float3.Mul(float3.New(0, 0, 1), force)
    
    EventHelper.AddListener(timer, "TimeEvent", function()
    -- Apply the force to the entity
    PhysicsAPI.AddForce(selfEntity, forwardForce)
    end)
    

    In this example, we are applying a 3000-Newton force to the entity in the forward direction of this entity's Z-axis.

  4. Save the script and enter Play Mode to playtest.

    Constant_force_added

Add an explosion force to an entity

Explosion force is a sphere that can be applied to an entity to simulate explosion effects. You can call the AddExplosionImpulseForce function to add explosion force to an entity.

  1. Add an entity to the scene and make sure the entity has a non-mesh collider attached to it.

    • In this tutorial, we use model Transverse_Roadblocks to the scene by selecting it via Scenery>Models>Basics.

    • The model comes with a mesh collider, so we turn off the mesh collider and add a sphere collider.

    Change_collider

  2. Add a rigidbody to the entity by choosing + Component>Gameplay>Rigidbody.

  3. Add the following script to the entity to apply a force to it. For more information on how to add a script, see Creating or adding scripts.

    local selfEntity = script:SelfEntity()
    
    local cmp = script:GetYaComponent("YaMovableComponent")
    local explosionPosition = cmp:GetPosition()
    
    EventHelper.AddListener(YaTime:WaitFor(5), "TimeEvent", function()
    PhysicsAPI.AddExplosionImpulseForce(selfEntity, 1000, explosionPosition, 15, 1000)
    end)
    

    In this example, we are using the function PhysicsAPI.AddExplosionImpulseForce to apply an explosion force to the rigid body. After a 5-second delay, an explosion force of 1000 Newtons is applied at the specified position, with a radius of 15, and moved along the y-axis as if to lift the entity upwards.

  4. Save the script and enter Play Mode to playtest.

    Explosion_force_added