Last active
July 9, 2020 08:35
-
-
Save 0xf0f0f0/594b92fe7fb8b039ea7001a33ffaf4e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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