Forum Discussion
mistayayo
7 months agoMember
Unable to spawn a ball above player after the ball collides with the floor.
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<typeofBallScript> {
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(newhz.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(newhz.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);
No RepliesBe the first to reply
Quick Links
- Horizon Developer Support
- Quest User Forums
- Troubleshooting Forum for problems with a game or app
- Quest Support for problems with your device
Other Meta Support
Related Content
- 9 months ago
- 10 months ago
- 2 years ago
- 4 years ago