flarb
10 months agoMHCP Partner
Is there a way to auto-grab items?
I want to make a player automatically grab an object when they bump into it instead of having to press the hold/grab button. Is there a way to do this? I don't see any method that would allow me to place an object in an avatar's hand.
I made a few assumptions. The object being a sword and that it is being force held on player enter. The main piece of code you are looking for is force hold line but this should work.
// these are the required imports from horizon/core import { Component, PropTypes, CodeBlockEvents, GrabbableEntity, Handedness } from 'horizon/core'; class ForceHoldObject extends Component<typeof ForceHoldObject > { static propsDefinition = { sword: { type: PropTypes.Entity }, }; // first we must connect to the player enter world event. This will give us a reference to the player when they enter the world. preStart() { this.connectCodeBlockEvent(this.entity, CodeBlockEvents.OnPlayerEnterWorld, data => this.OnPlayerEnterWorld(data)); } // once the player enters the world we can force hold the sword. // however, we must first ensure that the sword is set. // we do this by checking with an if statement. OnPlayerEnterWorld(player: Player) { if (this.props.sword) { // if this returns true we can force hold the sword this.props.sword.as(GrabbableEntity).forceHold(player, Handedness.Right, false) } else { // else we may want to know that it is not set so print a warning to the console. console.warn("sword is missing or null!"); } } } Component.register(ForceHoldObject);