Skip to content

Instantly share code, notes, and snippets.

@maxxfrazer
Created August 7, 2019 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxxfrazer/4e819e2fd33c4a723e9cc0f2a2623f5a to your computer and use it in GitHub Desktop.
Save maxxfrazer/4e819e2fd33c4a723e9cc0f2a2623f5a to your computer and use it in GitHub Desktop.
Collisions example for an Entity in RealityKit
extension CustomBox {
func addCollisions() {
guard let scene = self.scene else {
return
}
// Add the subscription for when this cube
collisionSubs.append(scene.subscribe(to: CollisionEvents.Began.self, on: self) { event in
// Get both CustomBox entities, if either entityA or entityB isn't a CustomBox
// then return becasue this is not the collision we're looking for
guard let boxA = event.entityA as? CustomBox, let boxB = event.entityB as? CustomBox else {
return
}
// Change the material color on the entity
boxA?.model?.materials = [SimpleMaterial(color: .red, isMetallic: false)]
boxB?.model?.materials = [SimpleMaterial(color: .red, isMetallic: false)]
})
collisionSubs.append(scene.subscribe(to: CollisionEvents.Ended.self, on: self) { event in
guard let boxA = event.entityA as? CustomBox, let boxB = event.entityB as? CustomBox else {
return
}
boxA?.model?.materials = [SimpleMaterial(color: .green, isMetallic: false)]
boxB?.model?.materials = [SimpleMaterial(color: .green, isMetallic: false)]
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment