Skip to content

Instantly share code, notes, and snippets.

@0xf0f0f0
Last active July 9, 2020 08:35
Show Gist options
  • Select an option

  • Save 0xf0f0f0/594b92fe7fb8b039ea7001a33ffaf4e8 to your computer and use it in GitHub Desktop.

Select an option

Save 0xf0f0f0/594b92fe7fb8b039ea7001a33ffaf4e8 to your computer and use it in GitHub Desktop.
cameraShake(counter = 10) {
this.startPosition = new THREE.Vector3;
this.startPosition.copy(scene.camera.position);
this.cameraShakeCounter = counter;
this.shakeOnce();
}
shakeOnce() {
let dist = 0.15;
let targetX = this.startPosition.x + Math.random() * dist * 2 - dist;
let targetY = this.startPosition.y + Math.random() * dist * 2 - dist;
let tweenMove = new TWEEN.Tween(scene.camera.position);
tweenMove.easing(TWEEN.Easing.Sinusoidal.InOut);
tweenMove.to({x:targetX, y:targetY}, 10);
tweenMove.start();
tweenMove.onComplete(() => {
this.cameraShakeCounter--;
if (this.cameraShakeCounter <= 0)
this.onShakeComplete();
else
this.shakeOnce();
});
}
onShakeComplete() {
this.stopShakeInstantly();
}
stopShakeInstantly() {
scene.camera.position.copy(this.startPosition);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment