VIRTUOS_Docs/sites/codesnippets/AlyxGrab.qmd
2026-01-07 22:07:11 +01:00

31 lines
2.3 KiB
Text

---
title: "Alyx Grab"
---
In Class: `BP_TraceController` | Based on VREs BP_TeleportTrace
To make grabbing more comfortable and accessible, I implemented a pull-grab mechanic inspired by Half Life Alyx. While the VR Expansion Plugin already implements logic for tossing objects towards the player, the template and many other resources rely on a line trace to get the object to toss. This results in multiple problems, above all the player having to aim very precisely and objects blocking each other.
To target these issues, I use a multi sphere trace to get all actors in range, saving them to an array. For each, we can check their angle to the hand via the dot product between the hand->forward and hand->object vectors and save them to an array as well. By getting the min of the of the dot product array, we can retrieve the correct object via the index (could've also used a map).
Function: `Trace For Tossable Objects`
```{=html}
<iframe width="100%" height="500" src="https://blueprintue.com/render/3xy3o_ve/" scrolling="no" allowfullscreen></iframe>
```
Function: `Get Angle To Hand`
```{=html}
<iframe width="100%" height="500" src="https://blueprintue.com/render/o_fr6mlk/" scrolling="no" allowfullscreen></iframe>
```
The trace relies on its own collision channel named TossableTrace, allowing for objects like cabinet doors to block this specific trace. As this blocks access to compartments smaller than the trace, there is a second smaller trace doing the same job (only if larger trace is blocked, might be further optimizable).
As it might not favorable for all components on the actor to be tossable, the Item Data Component (originally introduced for the [Inventory]()) holds an array of primitive components which are quickly added via a library macro on the respective actor (you can't choose primitive components via the dropdown on the component).
The ribbon between the object and the hand is drawn via a Niagara System getting passed an array of locations.
While I was aware of this technique from how the default UE5 VR Template renders its teleport-preview, there is also a [writeup by Steve Streeting](https://www.stevestreeting.com/2025/01/09/niagara-particles-setting-positions-manually/) if you need further information.
Note: The toss from VRE needs "Simulation Generates Hit Events" enabled to work correctly.