Created
August 7, 2019 20:29
-
-
Save maxxfrazer/4e819e2fd33c4a723e9cc0f2a2623f5a to your computer and use it in GitHub Desktop.
Collisions example for an Entity in RealityKit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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