Skip to main content
InaCentaur
Inspiring
May 5, 2025
Question

Why doesn't this TeleportComponent to different location script work?

 

So the idea is that the player interacts with a trigger gizmo and is teleported to a different location in the same world. Nothing happensd. 

 

import { PropTypes } from 'horizon/core';
import { Component, Entity, Player, Vec3, CodeBlockEvents } from 'horizon/core';

class TeleportComponent extends Component<typeof TeleportComponent> {
  static propsDefinition = {
    destination: { type: PropTypes.Entity }, // The entity that represents the destination
    triggerEntity: { type: PropTypes.Entity }, // The entity that triggers the teleportation
  };

  start() {
    // Check if the trigger entity exists
    if (this.props.triggerEntity!.exists()) {
      // Connect to the OnPlayerEnterTrigger event
      this.connectCodeBlockEvent(
        this.props.triggerEntity!,
        CodeBlockEvents.OnPlayerEnterTrigger,
        (player: Player) => {
          // Teleport the player to the destination
          this.teleportPlayer(player);
        }
      );
    }
  }

  teleportPlayer(player: Player) {
    // Get the destination position
    console.log("Teleporting player to destination:", this.props.destination!.position.get());
    const destinationPosition = this.props.destination!.position.get();

    // Teleport the player
    player.position.set(destinationPosition);
  }
}

Component.register(TeleportComponent);

1 reply

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