Forum Discussion

InaCentaur's avatar
8 months ago

Simple self-contained script - enter a trigger to activate fixed camera. Why doesn't this work?

Simple self-contained script - enter a trigger to activate fixed camera, at the position defined by cameraTarget. Why doesn't this work?
 
import { Entity } from 'horizon/core';
import * as hz from 'horizon/core';

class ChangeCameraOnTrigger extends hz.Component<typeof ChangeCameraOnTrigger> {
  static propsDefinition = {
    cameraTarget: { type: hz.PropTypes.Entity },
    duration: { type: hz.PropTypes.Number, default: 1 },
  };

  start() {
    this.connectCodeBlockEvent(this.entity, hz.CodeBlockEvents.OnPlayerEnterTrigger, (player: hz.Player) => {
      this.ChangeCamera(player);
    });
  }

  ChangeCamera(player: hz.Player) {
    console.log('Changing camera for player:', player);
    const targetPosition = this.props.cameraTarget!.position.get();
    const targetRotation = hz.Quaternion.lookRotation(targetPosition.sub(player.head.position.get()));

    // Use Player.focusUI to focus the camera on the target entity
    player.focusUI(this.props.cameraTarget!, { duration: this.props.duration! });
  }
}

hz.Component.register(ChangeCameraOnTrigger);

3 Replies