cancel
Showing results for 
Search instead for 
Did you mean: 

Batting cage tutorial issues with spawning ball above player location

TerianDiLun
Member

I am trying to add to the batting tutorial and I am having issues spawning the ball above the players head. I have implemented a couple different ways but they dont work. They will compile but the collision detection doesnt work. I have recieved errors such as failed to call method 'start' as well as having the code work with error detection but break once the player moves. This is my current code:

 

import * as hz from 'horizon/core';

type State = {
  originalPosition: hz.Vec3
}

class BallScript extends hz.Component<typeof BallScript> {

  static propsDefinition = {};

  originalPosition!: hz.Vec3;

  start() {
    // Listen for ball collisions with the ground.
    this.connectCodeBlockEvent(this.entity, hz.CodeBlockEvents.OnEntityCollision,
      (collidedWith: hz.Entity, collisionAt: hz.Vec3, normal: hz.Vec3,
        relativeVelocity: hz.Vec3, localColliderName: string, otherColliderName: string) => {

          // Move the ball back to its starting position above the local player's head.
          const localPlayer = this.world.getLocalPlayer();
          if (localPlayer) {
            const playerPosition = localPlayer.position.get();
            this.entity.position.set(playerPosition.add(new hz.Vec3(0, 2, 0))); // adjust the height as needed
          }

          // Reset the ball's velocity.
          this.entity.as(hz.PhysicalEntity).zeroVelocity();
        }
    );

    // Update the original position if the local player is already in the world.
    const localPlayer = this.world.getLocalPlayer();
    if (localPlayer) {
      this.updateOriginalPosition(localPlayer);
    }

    // Listen for the player entering the world and update the original position.
    this.connectCodeBlockEvent(this.entity, hz.CodeBlockEvents.OnPlayerEnterWorld, (player: hz.Player) => {
      if (player === this.world.getLocalPlayer()) {
        this.updateOriginalPosition(player);
      }
    });
  }

  // Update the original position of the ball to be above the player's head.
  private updateOriginalPosition(player: hz.Player) {
    const playerPosition = player.position.get();
    this.originalPosition = playerPosition.add(new hz.Vec3(0, 2, 0)); // adjust the height as needed
  }

  // Get the original position of the ball so that it respawns in the same place.
  override receiveOwnership(state: State, fromPlayer: hz.Player, toPlayer: hz.Player): void {
    this.originalPosition = state.originalPosition.clone(); // clone the Vec3 to avoid modifying the original state
    if (toPlayer === this.world.getLocalPlayer()) {
      this.updateOriginalPosition(toPlayer);
    }
  }

  // Pass the ball's original position to the new owner.
  override transferOwnership(fromPlayer: hz.Player, toPlayer: hz.Player): State {
    return { originalPosition: this.originalPosition.clone() }; // clone the Vec3 to avoid modifying the original state
  }
}

hz.Component.register(BallScript);
0 REPLIES 0