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
percentFinished
in the tick function using the keyword let
let percentFinished
- 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
percentFinished
is less than1
(the same thing as 100%).