ok so thanks to this video https://www.youtube.com/watch?app=desktop&v=IljIh9vpueQ
I realized the part I was missing was setting the entity to the owner. Which now makes sense when I tried your game Dungeon Delvers, it always started off in normal mode before the camera videw changes.
So I attached my SwipeGesture to a trigger, when the player enters the trigger I do
this.entity.owner.set(player). And when this is triggered, start() will run again
So now I have some sort of swiping going and can move an object side to side but it's only moving after the swipe action is done. It doesn't move like subway surfer where the moment the user move their finger left or right the player would move. Any suggestions?
import { CodeBlockEvents, Component, InteractionInfo, Player, PlayerControls, PropTypes, Vec3 } from 'horizon/core';
import { Gestures, SwipeDirection } from 'horizon/mobile_gestures';
class SwipeGesture extends Component<typeof SwipeGesture> {
static propsDefinition = {
character: { type: PropTypes.Entity },
};
private localPlayer: Player | null = null;
gestures = new Gestures(this);
start() {
this.localPlayer = this.world.getLocalPlayer();
if (!this.localPlayer) {
console.error("SwipeGesture: Could not get the local player.");
return;
}
// Check if the script is running on the server and exit if it is.
if (this.world.getLocalPlayer().id === this.world.getServerPlayer().id) {
// This log is for confirmation and can be removed if desired.
console.log("SwipeGesture component is correctly ignored on the server.");
this.connectCodeBlockEvent(this.entity, CodeBlockEvents.OnPlayerEnterTrigger, (player) => {
this.entity.owner.set(player);
})
return;
}
this.localPlayer.enterFocusedInteractionMode({ disableFocusExitButton: true });
this.gestures.onTap.connectLocalEvent(({ touches }) => {
console.log('tap', touches[0].current.screenPosition);
});
this.gestures.onLongTap.connectLocalEvent(({ touches }) => {
console.log('long tap', touches[0].current.screenPosition);
});
this.gestures.onSwipe.connectLocalEvent(({ swipeDirection }) => {
console.log('swipe', swipeDirection);
this.MovePlayer(swipeDirection);
});
this.gestures.onPinch.connectLocalEvent(({ scale, rotate }) => {
console.log('pinch', scale, rotate);
});
}
private MovePlayer(direction: SwipeDirection): void {
// Get the current position of the entity
const currentPosition = this.props.character?.position.get();
if (direction === SwipeDirection.Left) {
const newPosition = currentPosition!.add(new Vec3(-5, 0, 0));
this.props.character?.position.set(newPosition);
}
if (direction === SwipeDirection.Right) {
const newPosition = currentPosition!.add(new Vec3(5, 0, 0));
this.props.character?.position.set(newPosition);
}
}
}
Component.register(SwipeGesture);