ForceEffectComponent
A component that defines the forces that affect an entity, including custom forces that you define.
Declaration
struct ForceEffectComponentOverview
After you assign a ForceEffectComponent to an entity, the force effects in the component apply each physics update for as long as the component and force effects exist. Each ForceEffect updates sequentially while the physics system accumulates its forces. The center frame of each ForceEffect coincides with and moves alongside the entity’s position and orientation. To offset this center frame, set the position and orientation.
Create forces
Use ConstantForceEffect to apply a constant force in a direction:
let constantForceEffect = ForceEffect(effect: ConstantForceEffect(strength: 1, direction: [1, 0, 0]))RealityKit provides a set of useful forces you can use with ForceEffectComponent:
Create custom forces
To create a custom force effect, define a structure that implements ForceEffectProtocol, then create an instance of ForceEffectBase with your custom ForceEffectProtocol implementation:
// CustomForce implements ForceEffectProtocol, and has a custom parameter, thrustersActive, that determines how the system applies your force to a physics body.
let thrusterForce = CustomForce(thrustersActive: true)
// Create an instance of ForceEffectBase using your custom force.
let thursterForceEffect = ForceEffect(effect: thrusterForce)Apply forces
You can apply multiple forces to your entity at once by including them in ForceEffectComponent:
let forceEffectComponent = ForceEffectComponent(effects: [constantForceEffect, thrusterForceEffect])
entity.components.set(forceEffectComponent)