How to snap when one actor hits another actor in VR?

Dear everyone, I am trying to snap two actors when one actor hits another actor.
But I don’t have any idea. I am finding the blueprint to snap.
If you have any idea or experience, please help me.

Hi Happy Girl2019

Snapping objects in VR works the same as snapping an object in any other type of game scenario. Snap functionality is extremely common in VR as it is used when a player wants to pick up an object of some sorts.

I will take you through a simple example of snap functionality. I am going to attach an “arm” to my pawn, and when that arm intersects with an object, the object will snap to the end of that arm.

  1. Create a custom StaticMeshActor we will use for all of our interactive objects. I will call this MeshBase. This will be used as a base class so functionality can be shared between all of the world objects. For this example, we won’t actually need to add any code the MeshBase, but it is a good idea to have it anyway.

  1. Open up the StaticMeshActor we just created and we need to make configure it so it can see the arm that we will add to our pawn.

2.1) Set the static mesh component to moveable. This is important as our pawn is a movable object, if it remains static you cannot attach it.

2.2) Enable collision dectection of the mesh component. Set “GenerateOverlapEvents” to true (checked) and set the collision preset to “OverlapAllDynamic”.

  1. Now we can add our object(s) to the world. Simply drag the StaticMeshBase (my example is BP_MeshBase) into the world to create an instance. Select the object in the world and add your mesh/materials etc. It would be a good idea to rename your actor, I didn’t in my example to make it clear that it is the actor we just created.

  1. Next, I will open the pawn I am using and I will add a simple static mesh component with a cube mesh which will be used as the arm. I will also add a Scene Component as a child to the arm mesh which will be used as our snap target.
    When you attach two actors together, they will be joined by their pivots. Since I do not want the statue to snap to the middle of the arm, I will move the snap target Scene Component to the end.

We also need to add the same collision properties to the arm mesh component as we did to mesh base.
Set “GenerateOverlapEvents” true and Collision Preset = “OverlapAllDynamic”.

  1. While still in the pawn class, select the arm static mesh component and scroll to the bottom where you can see all the associated events with the component. Click the “+” on “OnComponentBeginOverlap” which will add the event to the event graph. The code is simple. We check if the overlapped actor inherits from our MeshBase and then we attach it to our snap target.

When I hit play, I can move my pawn to the object, when my arm “hits” the object, it will snap to the end of the arm.

2 Likes

When I hit play, you can see the object on the table.

When I move my arm into the object, it is attached to the end of the arm.

I know I have given a simple example. Snap functionality can get quite complicated very quickly, depending on what you want to do with the object once it has been grabbed.

Good luck :slight_smile:

1 Like