Forum Discussion

InaCentaur's avatar
InaCentaur
Start Member
9 months ago

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);