RealityKit – How disable the model's gravity programmatically?

Viewed 256

I want to programmatically disable gravity in RealityKit without any help of Reality Composer.

let arView = ARView(frame: .zero)
let boxAnchor = try! Experience.loadBox()
arView.scene.anchors.append(boxAnchor)

How to do that?

1 Answers

You can disable model's gravity using components[PhysicsBodyComponent]:

let arView = ARView(frame: .zero)
let boxAnchor = try! Experience.loadBox()
let entity = boxAnchor.steelBox! as! (Entity & HasPhysics)
    
entity.components[PhysicsBodyComponent] = .none

arView.scene.anchors.append(boxAnchor)

Or using physicsBody instance property:

entity.physicsBody = .none
Related