Hi, I'm trying to have an entity (and some attachments to it) to rotate. If I add the entity to content, add the attachment as a child entity, and set the entity as InputTargetComponent, then when I add a gesture ONLY the entity rotates and NOT the attachments (added as child entities).
If I add a parent entity with let parentEntity = ModelEntity(), add my entity to the parentEntity, then add the attachments to an entity (which is now a child of the ModelEntity) and set the ModelEntity as InputTargetComponent then the whole thing rotates (including attachments)
I'm sure there must be a bug, why would it work only with an added ModelEntity?
Anyway, bug or not a bug, the problem I have now is that it rotates around the axes of the ModelEntity, not my primary entity, which is what I want.
Is there a way to set the ModelEntity axes to be the axes of my primary child entity so it rotates like I want?
What call should I use to move the axes where would I find the axes of the first child entity which should be the focus of my app?
Here is my code:
var body: some View {
RealityView { content, attachments in
// Add the initial RealityKit content
if let specimenentity = try? await Entity(named: "Immersive", in: realityKitContentBundle) {
let parentEntity = ModelEntity()
parentEntity.addChild(specimenentity)
content.add(parentEntity)
let entityBounds = specimenentity.visualBounds(relativeTo: parentEntity)
parentEntity.collision = CollisionComponent(shapes: [ShapeResource.generateBox(size: entityBounds.extents).offsetBy(translation: entityBounds.center)])
parentEntity.generateCollisionShapes (recursive: true)
parentEntity.components.set(InputTargetComponent())
if let Left_Hemisphere = attachments.entity(for: "Left_Hemisphere") {
//4. Position the Attachment and add it to the RealityViewContent
Left_Hemisphere.position = [-0.5, 1, 0]
specimenentity.addChild(Left_Hemisphere)
}
}
} attachments: {
Attachment(id: "Left_Hemisphere") {
//2. Define the SwiftUI View
Text("Left_Hemisphere")
.font(.extraLargeTitle)
.padding()
.glassBackgroundEffect()
}
}
.gesture(
DragGesture()
.targetedToAnyEntity()
.onChanged { value in
let entity = value.entity
var orientation = Rotation3D(entity.orientation(relativeTo: nil))
var newOrientation: Rotation3D
if (value.location.x >= lastGestureValueX) {
newOrientation = orientation.rotated(by: .init(angle: .degrees(0.5), axis: .y))
} else {
newOrientation = orientation.rotated(by: .init(angle: .degrees(-0.5), axis: .y))
}
entity.setOrientation(.init(newOrientation), relativeTo: nil)
lastGestureValueX = value.location.x
orientation = Rotation3D(entity.orientation(relativeTo: nil))
if (value.location.y >= lastGestureValueY) {
newOrientation = orientation.rotated(by: .init(angle: .degrees(0.5), axis: .x))
} else {
newOrientation = orientation.rotated(by: .init(angle: .degrees(-0.5), axis: .x))
}
entity.setOrientation(.init(newOrientation), relativeTo: nil)
lastGestureValueY = value.location.y
}
)
}
}