LookAt
A component that makes the current object's orientation follow a target's position or orientation.
| Type | Name | Interface Description |
|---|---|---|
| Variables | directionAim: LookAtDirection | • Function: Gets the direction of the object that will point to the target. |
| Variables | directionUp: LookAtDirection | • Function: Gets the direction of the object that will be aligned with the world up direction. |
| Variables | mode: LookAtMode | • Function: Gets the mode for the look-at calculation. |
| Variables | offsetRotation: Vector3f | • Function: Gets An additional rotation applied after the look-at calculation. The value is in Euler angles. |
| Variables | target: Transform | null | • Function: Gets the transform of the target object to look at. |
| Variables | worldUp: LookAtWorldUp | • Function: Gets the world-up vector for the look-at calculation. |
| Functions | constructor() |
Examples
constructor()
let obj = new APJS.LookAt();
Use Case
Object always faces the camera (billboard effect)
@component()
export class LookAtCamera extends APJS.BasicScriptComponent {
onUpdate(deltaTime: number): void {
const transform = this.getSceneObject().getTransform();
const children = this.getSceneObject().parent?.getChildren() || [];
// Find camera in scene
for (const child of this.getSceneObject().parent?.getChildren() || []) {
const cam = child.getComponent("Camera") as APJS.Camera;
if (cam) {
const camPos = child.getTransform().getWorldPosition();
const myPos = transform.getWorldPosition();
const dir = new APJS.Vector3f(
camPos.x - myPos.x,
0,
camPos.z - myPos.z
).normalize();
const angle = Math.atan2(dir.x, dir.z) * 180 / Math.PI;
transform.localEulerAngles = new APJS.Vector3f(0, angle, 0);
break;
}
}
}
}