Unity VR – Bow and Arrow Part 3

We have our Bow working and we’re able to shoot Arrows. What else could we want?

Polish

One of the highlights about VR development is how immersive we can make our experiences. In order to amp up the immersion, we’ll need to add things like sound and haptic feedback. As an extra bonus, we’ll also add some particle effects and slight optimizations. 

Haptic Feedback

When we’re pulling the string back on the bow, we should give our players some feedback about the strength of their arrow shot. The further back the string is, the more our haptics should vibrate. To do that, we’re going to add some lines to the PullInteraction.

In order to add haptic feedback, we need to first grab the ActionBasedController component. Once we have this component, we can then call a function called SendHapticImpulse to vibrate the controller. In order to extract the ActionBasedController, we use the IXRInteractor from before, which will also correlate with whichever controller is currently pulling the string. 

Finally, we want to update this anytime the String is being pulled and update, so we add a call to it in the ProcessInteractable method.

String Release Sound

Next we need to add some audio to our String bow releasing.

  • Select the String
    • Add Component – Audio Source
      • Change Spatial Blend to 3D
      • Add a Bow Release sound to the Audio Clip (One can be found easily on https://freesound.org/)
Now that our Audio Source is set up, let’s add a few lines to our code.

Although not explicitly shown, I’ve added a variable called _audioSource that grabs the Audio Source component. I then add a new method called PlayReleaseSound. Initially it calls Stop() to prevent multiple release sound being played at the same time, then Play() to simply play it! Finally, this method is called in the Release function.

Particles and Trail for Arrows

Let’s spice up the Arrow. What we want to do is add some sparks flying off the tail of the Arrow as well as a trail so we can easily track our Arrows. 

  • Select Arrow Prefab
    • Right click the main object and add a Particle System
    • Select Particle System
      • Duration = 5
      • Start Delay = 0
      • Start Lifetime = 0.2
      • Start Speed = -0.1, -0.3
      • Start Size = 0, .05
      • Simulation Space = World
      • Emission
        • Rate over Time = 10
        • Rate over Distance = 15
      • Shape
        • Cone
        • Angle = 25
        • Radius = 0.06
      • Color Over Time
        • Opacity for second marker = 0
      •  Size over Lifetime
        • Decline Line from 1 to 0
      •  Renderer
        • Material = ArrowTrail
    • Select Particle System
      • Add Component – Trail Renderer
        • Width = Decrease over time from 0.05 to 0
        •  Time = 0.2
        • Material = ArrowTrail

Now that the Particle System and Trail Renderer are setup, we need to call them at the proper time in our code.

Opening the Arrow script, we need to add the following.

 

The added code will first grab the new components that we need and store them. It will then need to turn on these new components when the arrow is released and when the Stop method is called. 

Nothing too crazy, but the added effects really help to enhance the experience.

Conclusion

When it comes to polish, there is a never ending list of things someone can add to their project. For this tutorial, I just wanted to give a few ideas of what could be added in order to improve the immersive experience for our players. 

We could still add things like additional sound for pulling back the string or hitting targets with arrows, but the new Unity LTS is out and I need to get working on producing new tutorials covering all the fun features added in the most recent version of Unity.

Hope this helped and I’ll see you in the next one!