- Home /
Audio playing if user (The actual person) is moving
I'm trying to find out how to keep an audio playing if the user is moving (with the device in their pocket). And how to stop the audio when the user stops moving with his device.
How would i go about achieving this? This is my code so far, it doesn't work at all. The audio just plays no matter what. Can't even stop the audio with my pause/stop button.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayButton : MonoBehaviour
{
public GameObject chapter1;
public Vector3 userMovement;
void FixedUpdate()
{
chapter1 = GameObject.Find("Chapter 1");
userMovement = Input.acceleration;
if (userMovement.x > 0 || userMovement.y > 0 || userMovement.z > 0 && !chapter1.audio.isPlaying)
{
chapter1.audio.Play();
}
}
public void OnClick( dfControl control, dfMouseEventArgs mouseEvent )
{
// Add event handler code here
chapter1.audio.Play();
}
}
Answer by FinKone · Feb 27, 2014 at 05:23 PM
Could use two vector 3s. Have one as currentPosition, and lastPosition, then play the sound if current doesnt equal last, and at the end update last to the current. Have current tracking the objects current position and have last update to current. Not the most accurate way but simple.
Sorry, but this does not answer my question. I want the audio to play if the device is actually moving (the actual real life person moving)
Answer by roojerry · Feb 27, 2014 at 05:55 PM
Look into Gyroscope.userAcceleration.
I would assume you could check for some sustained acceleration over time (with some tolerance), to determine if the user is moving with their device. It may take some tweaking to get this right.
Thank you for your reply.
I have tried to use Gyroscope.userAcceleration but to no avail. I'm using android, if that matters.
Your answer
Follow this Question
Related Questions
move gameObject on z dependently on iOS device movement on x axis... 1 Answer
Virtual Reality: Move player when mobile moves 0 Answers
Mobile Movement Choppy in 2d endless runner 0 Answers
Android Shake 0 Answers
How can I calibrate phone accelerometer? 0 Answers