Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

[SOLVED] Android - Vibrate Phone on touch hold.

Discussion in 'Scripting' started by Spectra_7, Dec 17, 2019.

  1. Spectra_7

    Spectra_7

    Joined:
    Oct 17, 2017
    Posts:
    33
    Hi.

    I have a game where the player has to press and hold the screen.
    What I want is to vibrate the phone as long as player is touch holding the screen. Is this possible?

    Currently, phone vibrates for 1 sec.

    Thanks for any help in advance.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    I've never played with the vibrate feature on a phone, but it's possible it may be limited to avoid abuse. Continued vibrate could be a battery drainer, so you might only be able to trigger it every so often.

    Or, you may have to make the call repeatedly in a coroutine or update while the screen is touched.

    But I would still caution only triggering it every so often.
     
  3. Spectra_7

    Spectra_7

    Joined:
    Oct 17, 2017
    Posts:
    33
    When I am using Update, it resets the vibrator to start of every frame.

    Sorry, I am script noob.

    EDIT-------------------------------------------------------------------

    OK I solved this. Credits to ruzrobert.

    1. First get this script: https://gist.github.com/ruzrobert/d98220a3b7f71ccc90403e041967c46b
    2. Then make sure to add the namespace on top of the script. That is "using RDG"
    3. Here is simple Update Script that vibrates phone on touch and hold.
    Code (CSharp):
    1. private void Update()
    2. {
    3.     if (Input.GetMouseButton(0))
    4.     {
    5.         Vibration.Vibrate(10000, -1, false);
    6.     }
    7.     else if (Input.GetMouseButtonUp(0))
    8.     {
    9.         Vibration.Cancel();
    10.     }
    11. }
    But as Brathnann said, continues vibration should be avoided.
     
    Last edited: Dec 18, 2019