In this tutorial, you’ll learn how to trigger device vibration in Unity. This solution will take effect on any Android and iOS devices. Unity is a robust development engine with many features but when it comes to vibration there is a little more flexibility needed.
The official documentation has this example which we have to improove a bit to serve better for our needs.
Step 1 (Add Vibration Class)
Create New C# Script inside your project (Assets folder), name it Vibration.cs, paste the code bellow and save it.
Step 2 (Add a Manager)
Create another C# Script inside your project (Assets folder), name it EffectsManager.cs, paste the code bellow and save it, then attach it to your camera.
Step 3 (Use it)
Use it in your project, for example like this:
FindObjectOfType<EffectsManager>().VibrationWithDelay(60, .1f); // #param1 Duration, #param2 Delay
- FindObjectOfType – will find EffectsManager.cs script attached to your Camera.
- VibrationWithDelay – this is a function inside the EffectsManager.cs Script.
- #param1 (60) – specify the vibration duration.
- #param2 (.1f) – specify the delay before vibration start.
Here you can play with #param1 and #param2. Optimal values for Vibration can be set like this:
FindObjectOfType<EffectsManager>().VibrationWithDelay(50, .1f); // short & quick vibration FindObjectOfType<EffectsManager>().VibrationWithDelay(2000, .1f; // longer & quick vibration
See you in next article!
Best Regards, Lucas