2

I have an ModelEntity animation that move from point A to point B and takes a while to complete. When the user taps on the ModelEntity I would like to add a shrinking animation to the ModelEntity as well.

I tried adding the scale animation directly to the ModelEntity view .move but the problem there is I current transform of the model is where the model is expected to end. Causing the ModelEnity to jump to the end of the animation.

var transform = modelEntity.transform 
transform.scale *= factor
modelEntity.move(to: transform, relativeTo: modelEntity.parent, duration: duration) // will not work because the translation of the transform is already at the end of the animation

Is there a way to add a scaling animation to ModelEntity that is already in the middle of a different animation and make them work together?

mabarif
  • 241
  • 2
  • 10

2 Answers2

2

Tested with Xcode 14.2. :

import RealityKit
import CoreGraphics

extension Entity {
    
    func scaleAnimated(with value: SIMD3<Float>, duration: CGFloat) {
        
        var scaleTransform: Transform = Transform()
        scaleTransform.scale = value
        self.move(to: self.transform, relativeTo: self.parent)
        self.move(to: scaleTransform, relativeTo: self.parent, duration: duration)
        
    }
    
}

Usage:

var scaleTransform: Transform = Transform()
        
if exampleEntity.scale.x == 1.0 {
   exampleEntity.scaleAnimated(with: [0.012, 0.012, 0.012], duration: 1.0)
} else {
    exampleEntity.scaleAnimated(with: [1.0, 1.0, 1.0], duration: 1.0)
}
        
Darkwonder
  • 1,149
  • 1
  • 13
  • 26
1

This was a solution to my problem:

var transform = modelEntity.transform 
transform.scale *= factor
modelEntity.move(to: modelEntity.transform, relativeTo: modelEntity.parent)
modelEntity.move(to: transform, relativeTo: modelEntity.parent, duration: duration)

It looks like the problem was that I needed to get the entity to the position it was in at the middle of the animation. For some reason when you call the .move method it expects to start where it should have ended up regardless of the duration.

mabarif
  • 241
  • 2
  • 10