Mobile Vibration
You can absolutely add a small vibration (haptic feedback) when a player taps or clicks a GameObject in Unity. The exact method depends on the platform you’re targeting, but the setup is straightforward.
Below is a clean, reliable approach for mobile (Android/iOS) and a fallback for PC (no real vibration, but you can simulate with gamepad rumble).
📱 Mobile Vibration (Android & iOS)
Unity provides a simple built‑in vibration API through:
Handheld.Vibrate();
✔️ Step 1: Add a script to your clickable GameObject
using UnityEngine;
public class TapVibration : MonoBehaviour
{
private void OnMouseDown()
{
// Trigger vibration
Handheld.Vibrate();
}
}
Notes
- Works on Android automatically.
- On iOS, vibration works only if the device supports it.
OnMouseDown()works for 2D and 3D objects with a collider.
🎮 Optional: Stronger / Custom Vibration (Android Only)
If you want more control (duration, intensity), you can use Android’s vibration API:
using UnityEngine;
public class TapVibration : MonoBehaviour
{
AndroidJavaObject vibrator;
void Start()
{
if (Application.platform == RuntimePlatform.Android)
{
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
vibrator = activity.Call<AndroidJavaObject>("getSystemService", "vibrator");
}
}
private void OnMouseDown()
{
if (Application.platform == RuntimePlatform.Android)
{
vibrator.Call("vibrate", 50); // 50ms vibration
}
else
{
Handheld.Vibrate();
}
}
}
🕹️ PC / Gamepad Vibration (if using controller)
If you want vibration on a connected controller:
using UnityEngine;
using UnityEngine.InputSystem;
public class TapVibration : MonoBehaviour
{
private void OnMouseDown()
{
if (Gamepad.current != null)
{
Gamepad.current.SetMotorSpeeds(0.3f, 0.3f);
Invoke(nameof(StopVibration), 0.1f);
}
}
void StopVibration()
{
Gamepad.current?.SetMotorSpeeds(0, 0);
}
}
🎯 Quick Setup Checklist
- Add a Collider2D or Collider to your GameObject.
- Add the script to the same GameObject.
- Make sure your scene has a Camera with a Physics Raycaster (for UI) or just a normal camera for 2D/3D objects.
- Test on a real device — vibration doesn’t work in the editor.
If you want, I can help you:
- Trigger vibration only on UI buttons
- Add cooldown so vibration doesn’t spam
- Use the new Input System for touch detection
- Add haptics for iOS using the Taptic Engine
Just tell me what style you want your game to feel like.
Comments