A component that manages character movement.
To use a character controller, add a `CharacterControllerComponent` to your entity to make it a character entity.
Use ``Entity/moveCharacter(by:deltaTime:relativeTo:collisionHandler:)`` to move your character and respond to collisions, or ``Entity/teleportCharacter(to:relativeTo:)`` to place your character instantaneously in 3D space.
> Note: ``PhysicsBodyComponent`` and ``CollisionComponent`` are incompatible with `CharacterControllerComponent`, and RealityKit deactivates them if you add them to the same entity.
## Handle collision
Character entities are capsular, and you can specify their height and radius in the component's initializer.
A character's capsule shape aligns with its ``upVector`` so that the top and bottom of the capsule pass through that direction vector.
Although characters don't have a `CollisionComponent`, they can still interact with colliders.
The collision handler in `moveCharacter(by:deltaTime:relativeTo:collisionHandler:)` allows you to respond to collisions with solid colliders in your scene.
```swift
entity.moveCharacter(by: velocity * deltaTime, deltaTime: deltaTime, relativeTo: nil) {
event in
// Your character collided with `event.hitEntity`.
}
```
To handle collisions with colliders that have a ``CollisionComponent/mode-swift.property`` of ``CollisionComponent/Mode-swift.enum/trigger``, subscribe to the ``CollisionEvents/Began`` event for your ``RealityView``.
This event doesn't occur for solid collisions with your character, but RealityKit invokes it when your character enters a trigger.
This is useful for collecting coins, claiming checkpoints, activating enemy behavior when your character enters an area, and many other interactions within games.
## Update your character
A common use case for `CharacterControllerComponent` is to control a character in a video game.
Video games have update loops where characters and game logic update once each frame, allowing them to respond to player input in real time.
`CharacterControllerComponent` works well in such a setup.
To move your character in response to player input, subscribe to ``PhysicsSimulationEvents/WillSimulate`` on ``RealityViewContent`` for your scene.
The event object contains information like the time delta since the last update, and you can treat this callback as the update loop for your game.
> Note: You can also use ``SceneEvents/Update`` to run code for each frame, but avoid using it to control physics-based motion in your scene.
Read the values from ``CharacterControllerStateComponent`` to accumulate forces (such as gravity) and to check whether the character is on the ground.
RealityKit calculates ``CharacterControllerStateComponent/isOnGround`` and ``CharacterControllerStateComponent/velocity`` only after you call `moveCharacter(by:deltaTime:relativeTo:collisionHandler:)`, so it's a good idea to call this function at each update.
```swift
let gravity: SIMD3<Float> = [0, -50, 0]
let jumpSpeed: Float = 10
content.subscribe(to: PhysicsSimulationEvents.WillSimulate.self, on: playerEntity) {
event in
let deltaTime: Float = event.deltaTime
var velocity: SIMD3<Float> = .zero
var isOnGround: Bool = false
// RealityKit automatically adds `CharacterControllerStateComponent` after moving the character for the first time.
if let ccState = playerEntity.components[CharacterControllerStateComponent.self] {
velocity = ccState.velocity
isOnGround = ccState.isOnGround
}
if !isOnGround {
// Gravity is a force, so you need to accumulate it for each frame.
velocity += gravity * deltaTime
} else if myPlayerInput.jump {
// Set the character's velocity directly to launch it in the air when the player jumps.
velocity.y = jumpSpeed
}
playerEntity.moveCharacter(by: velocity * deltaTime, deltaTime: deltaTime, relativeTo: nil) {
event in
print("playerEntity collided with \(event.hitEntity.name)")
}
}
```
The following video shows a character entity jumping in response to player input:
@Video(
source:character_controller_component_jump.mp4,
alt: "A video of a white capsule jumping on a flat gray plane in response to player input.",
poster:character_controller_component_jump_poster.png
)
Members (21)
get · iOS 15.0+
slopeLimit
TypeScript
get slopeLimit(): number
Swift declaration
var slopeLimit: Float
The slope limit expressed as a limit angle in radians.
This value represents the maximum slope that the character can move over. RealityKit applies this value
to characters that are walking on static objects, but not when walking on kinematic or dynamic objects.
Changing this value after the CharacterControllerComponent has been created and added to Entity
has no effect.
set · iOS 15.0+
slopeLimit
TypeScript
set slopeLimit(value: number)
Swift declaration
var slopeLimit: Float
The slope limit expressed as a limit angle in radians.
This value represents the maximum slope that the character can move over. RealityKit applies this value
to characters that are walking on static objects, but not when walking on kinematic or dynamic objects.
Changing this value after the CharacterControllerComponent has been created and added to Entity
has no effect.
get · iOS 15.0+
defaultHeight
TypeScript
static get defaultHeight(): number
Swift declaration
static let defaultHeight: Float
The default height for the capsule when there isn’t a provided value.
get · iOS 15.0+
defaultRadius
TypeScript
static get defaultRadius(): number
Swift declaration
static let defaultRadius: Float
The default radius for the capsule when there isn’t a provided value.
get · iOS 15.0+
collisionFilter
TypeScript
get collisionFilter(): CollisionFilter
Swift declaration
var collisionFilter: CollisionFilter
The character's collision filter.
For more information on using collision filters, see <doc://com.apple.documentation/documentation/realitykit/controlling_entity_collisions_in_realitykit>.
set · iOS 15.0+
collisionFilter
TypeScript
set collisionFilter(value: CollisionFilter)
Swift declaration
var collisionFilter: CollisionFilter
The character's collision filter.
For more information on using collision filters, see <doc://com.apple.documentation/documentation/realitykit/controlling_entity_collisions_in_realitykit>.
get · iOS 15.0+
defaultUpVector
TypeScript
static get defaultUpVector(): SDKHandle
Swift declaration
static let defaultUpVector: SIMD3<Float>
The default up vector for the capsule when there isn’t a provided value.
get · iOS 15.0+
defaultSkinWidth
TypeScript
static get defaultSkinWidth(): number
Swift declaration
static let defaultSkinWidth: Float
The default skin width for the capsule when there isn’t a provided value.
get · iOS 15.0+
defaultStepLimit
TypeScript
static get defaultStepLimit(): number
Swift declaration
static let defaultStepLimit: Float
The default step limit for the capsule when there isn’t a provided value.
get · iOS 15.0+
defaultSlopeLimit
TypeScript
static get defaultSlopeLimit(): number
Swift declaration
static let defaultSlopeLimit: Float
The default slope limit for the capsule when there isn’t a provided value.
get · iOS 15.0+
height
TypeScript
get height(): number
Swift declaration
var height: Float
The capsule height.
The capsule height includes radii and should be specified the entity's coordinate system.
set · iOS 15.0+
height
TypeScript
set height(value: number)
Swift declaration
var height: Float
The capsule height.
The capsule height includes radii and should be specified the entity's coordinate system.
get · iOS 15.0+
radius
TypeScript
get radius(): number
Swift declaration
var radius: Float
The capsule radius.
Specify this value in the entity's coordinate system.
set · iOS 15.0+
radius
TypeScript
set radius(value: number)
Swift declaration
var radius: Float
The capsule radius.
Specify this value in the entity's coordinate system.
get · iOS 15.0+
upVector
TypeScript
get upVector(): SDKHandle
Swift declaration
var upVector: SIMD3<Float>
The y-axis direction relative to the physics origin.
Rotates the object so that the vertical height is along the up vector.
Normalize and specify the vector in *physics space*,
the coordinate system of the physics simulation.
set · iOS 15.0+
upVector
TypeScript
set upVector(value: SDKHandle)
Swift declaration
var upVector: SIMD3<Float>
The y-axis direction relative to the physics origin.
Rotates the object so that the vertical height is along the up vector.
Normalize and specify the vector in *physics space*,
the coordinate system of the physics simulation.
get · iOS 15.0+
skinWidth
TypeScript
get skinWidth(): number
Swift declaration
var skinWidth: Float
An added tolerance around the character capsule.
A small skin, known as the *contact offset*, is maintained around the controller's volume to avoid
rounding and precision issues with collision detection. Specify this value relative to the entity's
coordinate system.
set · iOS 15.0+
skinWidth
TypeScript
set skinWidth(value: number)
Swift declaration
var skinWidth: Float
An added tolerance around the character capsule.
A small skin, known as the *contact offset*, is maintained around the controller's volume to avoid
rounding and precision issues with collision detection. Specify this value relative to the entity's
coordinate system.
get · iOS 15.0+
stepLimit
TypeScript
get stepLimit(): number
Swift declaration
var stepLimit: Float
The maximum obstacle height that the controller can move over.
Specify this value relative to the entity's coordinate system.
Changing this value after the CharacterControllerComponent has been created and added to Entity
has no effect.
set · iOS 15.0+
stepLimit
TypeScript
set stepLimit(value: number)
Swift declaration
var stepLimit: Float
The maximum obstacle height that the controller can move over.
Specify this value relative to the entity's coordinate system.
Changing this value after the CharacterControllerComponent has been created and added to Entity
has no effect.
init · iOS 15.0+
init
TypeScript
static init(): CharacterControllerComponent
Swift declaration
init()
Creates a character controller component using default values.
Skipped declarations
1 declarations were excluded from these bindings. Each reason comes from the generator or the iOS SDK compiler.