ThirdPersonCamera
new ThirdPersonCamera(physics : Object, options : Object)ThirdPersonCamera - Third-person camera with orbit and collision
Extends PhysicsCamera to provide third-person camera controls. Handles offset positioning, orbit rotation, pointer lock, and collision avoidance.
Constructor Parameters
physicsObjectoptionsoptionalObjectDefault is
{}.canvasoptionalHTMLCanvasElementCanvas element for pointer locktargetoptionalTHREE.Object3DPlayer mesh to follow (auto-detects body index from mesh.userData)playerBodyIndexoptionalnumberExplicit body index to follow (overrides auto-detection)offsetoptionalTHREE.Vector3Camera offset from targetlockRotationoptionalbooleanLock camera rotation to character movement
Default isfalse.sensitivityoptionalObjectMouse sensitivity
Default is{ x: 0.5, y: 0.5 }.fovoptionalnumberField of view
Default is70.
Example
import { ThirdPersonCamera } from '@three-blocks/pro';
const camera = new ThirdPersonCamera(physics, {
canvas: renderer.domElement,
target: playerMesh,
offset: new THREE.Vector3(0, 2, -5),
sensitivity: { x: 0.5, y: 0.5 },
});
camera.on('lock', () => console.log('Pointer locked'));
// In animate loop
camera.update(delta);
renderer.render(scene, camera);Properties
# .canvas : HTMLCanvasElement
Canvas element for pointer lock.
# .offset : THREE.Vector3
Camera offset from target.
# .lockRotation : boolean
Whether camera rotation is locked to character movement.
# .sensitivity : Object
Mouse sensitivity for orbit rotation.
# .locked : boolean
Whether pointer lock is currently active.
# .yaw : number
Current yaw angle in radians.
# .yaw :
Set yaw angle in radians.
Methods
on#
on(event : 'lock'|'unlock', callback : function) : thisRegister an event callback.
Parameters
event'lock' | 'unlock'callbackfunctionReturns
this — For chainingoff#
off(event : 'lock'|'unlock', callback : function) : thisRemove an event callback.
Parameters
event'lock' | 'unlock'callbackfunctionReturns
this — For chainingupdate#
update(delta : number)Update camera state. Call each frame.
Parameters
deltanumbersetOffset#
setOffset(offset : THREE.Vector3) : thisSet camera offset from target.
Parameters
offsetTHREE.Vector3Returns
this — For chainingsetLockRotation#
setLockRotation(locked : boolean) : thisSet whether rotation is locked to character movement. When enabling lock, automatically snaps camera behind the target and releases pointer lock since mouse rotation is disabled.
Parameters
lockedbooleanReturns
this — For chaininglock#
lock()Manually request pointer lock. Does nothing if lockRotation is enabled.
unlock#
unlock()Exit pointer lock.
dispose#
dispose()Clean up resources and event listeners.