The bow and arrow are considered as high damage and stealth weapon in most games today. If you have played games like FarCry or Assassins Creed you will know how amazing they are to play with. With VR, we can take that experience to the next level. With the freedom to choose the type of bow, visual effects, sound effects, etc. we can create an immersive experience for the user.
This blog was inspired by Unity's prototype series.
You must have a basic knowledge of installing the XR Interaction Toolkit, its components and properties, working with prefabs, and the basics of C# as well.
You should know how to set up a scene with a ground plane and an XR Rig. If not, this tutorial will help you get started with the XR Interaction Toolkit. You can learn more about prefabs here.
Note: This was built and tested in Unity version 2020.3.19 LTS and XR Interaction Toolkit version 1.0.0-pre.8.
Let's start by importing the models. We'll set up the Bow in such a way that the bow model can be changed easily without affecting any of the interactions.
The reason for using the LineRenderer with 3 points is that its value at index 1 can be used to visualize the stretching effect of the string and the attachment point of the arrow.
With the Bow GameObject created, next up is the Arrow GameObject.
<div class=callout><div class="callout-emoji">💡</div><p style="margin-bottom:0px;">Note: If you change the orientation of the arrow prefab, make sure to change the transform of the Tip GameObject as well!<p></div>
In the next section, we will learn how to add various interactions to the Bow and Arrow GameObject.
There are 4 main features:
These features will enable us to have the bow and arrow interaction in VR, as soon as we combine them.
This section focuses on the string interaction. Answering a few of the following questions will give you a better understanding.
Let's code the string interaction that will enable us to pull the string between two fix points that we have already created above and return a value between 0 and 1 based on the position of the string.
In the next section 3.1.2 we have the breakdown of the code as well, so don't worry if you are not able to understand the math behind the code immediately.
Create a new C# script and name it StringInteraction and copy the following code. The code will return a value for the variable PullAmount when the string is grabbed and pulled. The value of PullAmount is based on the interactor's position and string position in world space.
This section will help you understand the code, feel free to skip to Section 3.1.3 (Testing) if you understood the program.
The script inherits from XRBaseInteractable class.
Variable NameTypeUsestringStartPointis of type TransformTo store the transform values of the starting point of the stringstringEndPointis of type TransformTo store the transform values of the ending point of the stringstringInteractoris of type XRBaseInteractorTo assign the interactor that will be pulling the sting. In this case, it can either be the left hand or the right hand.pullPoistionis of type Vector3To store the position of the VR hand after pulling the stringpullDirectionis of type Vector3To store the direction of pulltargetDirectionis of type Vector3To store the direction the bow is facingPullAmountis a Property of type floatTo store the pull value privately in this class. It is read-only and other classes can read the pull value using this property.StringStartPointis a Property of type Vector3To store the local position of the variable stringStartPoint. It is read-only and other classes can read the local position value using this property.StringEndPointis a Property of type Vector3To store the local position of the variable stringEndPoint. It is read-only and other classes can read the local position value using this property.
Before proceeding to the next steps, let's test if this works as intended.
Note: While developing this project I made use of the XR Simulator for testing. I felt it speeds up the development process:
You will not be able to see the string getting pulled visually and that is because we are yet to implement that feature. In the next section, we will use the pullAmount value to move the line renderer to represent the pulling action.
🎉 With this we have completed the String Interaction, let's move on to the Bow Interaction.
If you've enjoyed the insights shared here, why not spread the word? Share the post with your friends and colleagues who might also find it valuable.
Your support means the world to us and helps us create more content you'll love.