To make the animation match the timing, we’ll calculate what percent of the way we are to the end time, and move the chest a proportional amount.
- Create a new variable called
percentFinishedin the tick function using the keyword let
- Set percentFinished to elapsedTime divided by animationTime
let percentFinished = this.elapsedTime / this.animationTime
- Create another variable called heightChange and set it to percentFinished times the amountToMoveUp
let heightChange = percentFinished * this.amountToMoveUp
- Instead of incrementing the chest, we’ll set its Y to where it started plus the change
this.entity.worldTransform.position.y = this.startingPosition.y + heightChange
- Change the condition of the second if statement to check if
percentFinishedis less than
1(the same thing as 100%).