This blog post is the continuation of the previous post where we've learned about setting up the prefabs and adding the string interaction component to it. If you haven't checked it out already, then do check out Part 1.
This section focuses on the overall bow interaction. The bow interaction involves:
In the next section 3.2.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.
Let's implement the bow interaction by writing the code for the same, so create a new C# script and name it BowInteraction and copy the following code. The code will update the line renderer and the transform of the GameObject String as per PullAmount.
This section will help you understand the code, feel free to skip to Section 3.2.3 (Testing) if you understood the program.
The script inherits from XRBaseInteractable class.
Variable NameTypeUsebowStringis of type LineRendererTo update the mid-point of the line rendered based on the pullAmountstringInteractionis of type StringInteractionTo get the value of PullAmountsocketTransformis of type TransformTo store the transform value of the socket interactor which is nothing but the transform of the GameObject StringBowHeldis a Property of type boolTo store the value as true if the bow is grabbed and false if it is not grabbed. It is read-only and other classes can read the boolean value.
Now, 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:
🎉 With this we have completed the Bow Interaction, let's move on to the Arrow 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.