Unreal Engine 4 (UE4) is a powerful engine where many amazing games and virtual reality (VR) experiences and games have been developed. An area that differs between VR and traditional games is how interactions between objects are done. For example in VR, you might interact with your head movement and your hand movements. While in a traditional video game, you would get close to an object and press a button. In this post, I am going to cover various ways of doing interactions in VR.
Distance Interaction with Head and Hands
Sometime in your VR project, you may want to be able to interact with an object from a distance. UE4 offers a useful tool to do this and it is called Raycast or Tracing. It is a Blueprint node that draws a line from one point to another in 3D space. When the line overlaps with an object, you can obtain information about the particular object as a result.
By using Tracing, you can create an elegant and simple system for interacting with objects in the world for the user. For example, when the user looks at an object it then moves or does an animation. While the object is responding to your gaze, you can press a button on your VR motion controller to perform an action.
Here is a Blueprint function of how you can start the system I mentioned above (using the VR template that comes with UE4.13.2+):
The two Blueprints are part of the same function; I just split it up for viewing purpose. The “World Objects” variable is of type EObject Type Query.
By combining the Detect for Gaze Overlap function with the Motion Controller trigger detection events, you can activate the action the gazed object performs. You just need to fill the Gazed Object Action function with your own logic.
Close-Up Interaction
For close-up interactions with objects, you would want to keep it simple. Try to avoid unnecessary physics and if possible omit physics altogether. VR is demanding to develop for already, there is no need to add more computations unless it is absolutely necessary.
For interaction without much physics, you can use the Events ActorBeginOverlap and ActorEndOverlap for Blueprints. If you are using C++, you can create a dynamic binding to the events in the constructor.
Blueprint Example
Here is an example of how to use the ActorBeginOverlap and ActorEndOverlap events. When the event happens, the other actor is filtered out unless it is what you are looking for. Afterward, the overlap action occurs if it is the object you are after.
C++ Example
Here is the C++ version of the Blueprint example above. The main difference is that you have to add the binding to the events in the constructor. For this example, I am using a new C++ class Actor call DummyActor.
DummyActor.cpp
ADummyActor::ADummyActor() { PrimaryActorTick.bCanEverTick = true; ... // bind ActorBeginOverlap and ActorEndOverlap this->OnActorBeginOverlap.AddDynamic(this, &ADummyActor::handleOnOverlapBegin); this->OnActorEndOverlap.AddDynamic(this, &ADummyActor::handleOnOverlapEnd); } void ADummyActor::handleOnOverlapBegin(class AActor* overlapped_actor, class AActor* other_actor) { FString other_actor_name = other_actor->GetName().ToLower(); if (other_actor_name.Contains("targetobject")) { overlapBeginAction(other_actor); } } void ADummyActor::overlapBeginAction(AActor* other_actor) { // do action for begin overlap } void ADummyActor::handleOnOverlapEnd(class AActor* overlapped_actor, class AActor* other_actor) { FString other_actor_name = other_actor->GetName().ToLower(); if (other_actor_name.Contains("targetobject")) { overlapEndAction(other_actor); } } void ADummyActor::overlapEndAction(AActor* other_actor) { // do action for end overlap }
As you can see, the C++ version requires more work than the Blueprint version. The advantage to the C++ version is you have access to the lower level API if you find yourself to need them. With Blueprint, you are limited to what there is. For more details about using Blueprint and C++, you can check out my post about VR development with UE4.
I hope you found this post helpful to you. If you found this post helpful, share it with others who may benefit from it.
If you have any questions or feel that I missed something important, feel free to leave a comment. To stay in touch, follow me on Twitter.
Happy developing!
Pingback: Managing Time Sensitive Events in Virtual Reality with Unreal Engine 4 - bright developers