Intro to 3D Game Development

0 of 193 lessons complete (0%)

Scripting Part 1: Moving Objects

Calculate the Percent Finished and Height Change

You don’t have access to this lesson
Please register or sign in to access the course content.

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.

  1. Create a new variable called percentFinished in the tick function using the keyword let
let percentFinished
  1. Set percentFinished to elapsedTime divided by animationTime
let percentFinished = this.elapsedTime / this.animationTime
  1. Create another variable called heightChange and set it to percentFinished times the amountToMoveUp
    • let heightChange = percentFinished * this.amountToMoveUp
  2. 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
  3. Change the condition of the second if statement to check if percentFinished is less than 1 (the same thing as 100%).