Skip to main content
InaCentaur
Inspiring
April 28, 2025
Question

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

SeeingBlue
Honored Contributor
April 28, 2025

Manipulating player cameras require local script. The object running the script must be locally scripted and have its ownership transferred to the player you wish to manipulate.

How to enable local execution mode

Explanation of local scripting

Upcoming Events : Performance Pro - UI Unleashed - Building Responsive & Performant Interfaces Thursday June 19th 5:00 PM PT - Register June Programming Calendar
InaCentaur
Inspiring
April 29, 2025

yes i tried that and still no camera change... 

SeeingBlue
Honored Contributor
April 29, 2025

Have you looked at the Camera API tutorial world?

Upcoming Events : Performance Pro - UI Unleashed - Building Responsive & Performant Interfaces Thursday June 19th 5:00 PM PT - Register June Programming Calendar