Skip to content

Instantly share code, notes, and snippets.

@tomshaw
Created January 10, 2022 20:03
Show Gist options
  • Save tomshaw/97c65dafbb2600798a74f8824448031c to your computer and use it in GitHub Desktop.
Save tomshaw/97c65dafbb2600798a74f8824448031c to your computer and use it in GitHub Desktop.
Make the mesh follow the mouse
onMouseMove(event) {
this.mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
this.mouse.y = - (event.clientY / window.innerHeight) * 2 + 1;
// Make the sphere follow the mouse
var vector = new THREE.Vector3(this.mouse.x, this.mouse.y, 0.5);
vector.unproject( this.camera );
var dir = vector.sub( this.camera.position ).normalize();
var distance = - this.camera.position.z / dir.z;
var pos = this.camera.position.clone().add( dir.multiplyScalar( distance ) );
this.mesh.position.copy(pos);
// Make the sphere follow the mouse
// mouseMesh.position.set(event.clientX, event.clientY, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment