Note : These events are triggered within a local script, on an object owned by the player, when the player touches the screen on a mobile device, or when they click on the screen on the desktop.
player.enterFocusedInteractionMode()
from a script in default execution mode, passing a reference to the player. You might want to do this in response to a player input, or an interaction with a button, or when the player enters a trigger zone.Note : Only custom input controls displayed via thePlayerControls.connectLocalInput
API remain visible.
// Register the "E" button as a custom input.
this.enterFocusInput = hz.PlayerControls.connectLocalInput(
hz.PlayerInputAction.RightGrip,
hz.ButtonIcon.Interact,
this,
);
// Add a callback handler that enters Focused Interaction mode
// when the button is pressed.
this.enterFocusInput.registerCallback(
(action: hz.PlayerInputAction, pressed: boolean) => {
if (pressed) {
this.entity.owner.get().enterFocusedInteractionMode();
}
},
);
OnPlayerEnteredFocusedInteraction
Codeblock event is broadcast on the server. You can handle this event with a script that runs in default execution mode, The event passes a parameter that contains details about which player entered Focused Interaction mode.this.connectCodeBlockEvent(
this.entity,
CodeBlockEvents.OnPlayerEnteredFocusedInteraction,
(player: hz.Player) => {
// Logic to handle a player entering Focused Interaction mode.
// You can activate an entity in the world, or you can change
// the player's camera position.
},
);
Event | Description |
---|---|
PlayerControls.onFocusedInteractionInputStarted | Fires on the first frame of a touch. This is the frame in which the player starts to touch the screen. |
PlayerControls.onFocusedInteractionInputEnded | Fires on the frame when a touch ends. This is the frame in which the player stops touching the screen. |
PlayerControls.onFocusedInteractionInputMoved | Fires on every frame between the started and ended events. This event doesn’t fire on the first or final frame of the touch. |
InteractionInfo
, which is defined as follows:InteractionInfo = {
// An index for differentiating between simultaneous inputs.
// The first input is 0.
interactionIndex: number;
// The screen position of the input normalized to the range
// (0,0) to (1,1).
screenPosition: Vec3;
// The origin point of a ray into the world generated from
// the touch.
worldRayOrigin: Vec3;
// The direction vector of a ray into the world generated from
// the touch.
worldRayDirection: Vec3;
};
interactionInfo
parameter is an array of interactions. This contains only a single item because multi-touch isn’t currently supported.Note : To future-proof your code against added support for multi-touch, check theinteractionIndex
to ensure that you only handle the touches that you intend.
this.connectLocalBroadcastEvent(hz.PlayerControls.onFocusedInteractionInputStarted, (data: {interactionInfo: hz.InteractionInfo[]}) => {
// Select the first item in the array, and check to ensure
// that it's the first touch.
var firstInteraction = data.interactionInfo[0
if (firstInteraction.interactionIndex != 0) {
return;
// Use the data in firstInteraction to execute gameplay logic.
// You could raycast into the world, or use screenPosition to
// calculate a per frame delta, and track swipe gestures.
});
this.connectLocalBroadcastEvent(hz.PlayerControls.onFocusedInteractionInputMoved, (data: {interactionInfo: hz.InteractionInfo[]}) => {
// Select the first item in the array, and check to ensure that
// it's the first touch.
var firstInteraction = data.interactionInfo[0];
if (firstInteraction.interactionIndex != 0) {
return;
}
// Use the data in firstInteraction to execute gameplay logic.
// You could raycast into the world, or use screenPosition to
// calculate a per frame delta and track swipe gestures.
});
this.connectLocalBroadcastEvent(hz.PlayerControls.onFocusedInteractionInputEnded, (data: {interactionInfo: hz.InteractionInfo[]}) => {
// Select the first item in the array, and check to
// ensure that it's the first touch.
var firstInteraction = data.interactionInfo[0];
if (firstInteraction.interactionIndex != 0) {
return;
}
// Use the data in firstInteraction to execute gameplay logic.
// You could raycast into the world, or use screenPosition to
// calculate a per frame delta and track swipe gestures.
});
player.exitFocusedInteractionMode()
, passing a reference to the local player.this.connectCodeBlockEvent(
this.entity,
CodeBlockEvents.OnPlayerExitedFocusedInteraction,
(player: hz.Player) => {
// Handle the player exiting Focused Interaction mode.
// You can reset their camera, or change world state.
},
);
// Register the "F" button as a custom input.
this.exitFocusInput = hz.PlayerControls.connectLocalInput(
hz.PlayerInputAction.RightSecondary,
hz.ButtonIcon.Drop,
this,
);
// Add a callback handler that exits Focused Interaction mode when
// the player presses the Exit button. Perform your cleanup when
// you receive the Exit event.
this.exitFocusInput.registerCallback(
(action: hz.PlayerInputAction, pressed: boolean) => {
if (this.isInFIMode) {
this.entity.owner.get().exitFocusedInteractionMode();
this.isInFIMode = false;
}
},
);
OnPlayerExitedFocusedInteraction
CodeBlock event fires either when you directly call the exit method, or when the player presses the dedicated exit button. You can handle this event to call any cleanup code (like resetting the player’s camera).let newTapOptions = {
...hz.DefaultFocusedInteractionTapOptions,
duration: 0.25,
startColor: hz.Color.blue,
endColor: hz.Color.white,
startScale: 0.5,
endScale: 1.2,
startOpacity: 0.8,
endOpacity: 0,
startRotation: 180,
endRotation: 0,
};
let newTrailOptions = {
...hz.DefaultFocusedInteractionTrailOptions,
length: 0.5,
startColor: hz.Color.blue,
endColor: hz.Color.white,
startWidth: 0.8,
endWidth: 0.1,
startOpacity: 0.8,
endOpacity: 0,
};
player.focusedInteraction.setTrailOptions(true, newTrailOptions);