- Home /
Accelerometer and drag
I'm trying to spin an object that must retain the force applyed by the accelerometer for quite some time. For some reasone the code is not working even if the same come works very well with swipes.
using UnityEngine;
using System.Collections;
public class GyroControls : MonoBehaviour
{
public GameObject m_objectToMove;
public GameObject m_sphereBackground;
public float drag = 1f;
private float _rotValue = 0;
public static GyroControls Instance{set;get;}
void Awake ()
{
// First we check if there are any other instances conflicting
if(Instance != null && Instance != this)
{
// If that is the case, we destroy other instances
Destroy(gameObject);
}
// Here we save our singleton instance
Instance = this;
// Furthermore we make sure that we don't destroy between scenes (this is optional)
DontDestroyOnLoad(gameObject);
}
void FixedUpdate()
{
ProcessInput(Input.acceleration);
ProcessMotion(Time.deltaTime);
ProcessPhysics(Time.deltaTime);
}
void ProcessInput(Vector3 dir)
{
_rotValue += dir.x;
}
void ProcessMotion(float deltaTime)
{
float _turnValue = _rotValue * deltaTime;
m_objectToMove.transform.Rotate(new Vector3(0,0,_turnValue));
m_sphereBackground.transform.Rotate( new Vector3(0,0,_turnValue));
}
void ProcessPhysics(float deltaTime)
{
// drag must be improved as function of _rotValue
if(_rotValue < -clamp)
_rotValue += drag;
else if(_rotValue > clamp)
_rotValue -= drag;
}
}
Answer by robertbu · Aug 19, 2013 at 06:03 PM
I'm going to take a guess here. I've only played with the an accelerometer a couple of times, and I don't have my Mac setup here to test my theory. An accelerometer should measure a change in velocity. So if I spin the device around I'm going to get some value as I speed up. If I then continue to rotate at the same speed, the acceleration will drop to zero (i.e. the velocity is not changing). As I stop spinning, I'm going to get an equal and opposite acceleration to the one I had to start with when I started spinning.If that is what you see with your accelerometer with then this line:
_rotValue += dir.x;
will increase as I start to rotate, but will drop back to something close to zero as I come to a stop. As I come to a stop, the opposite acceleration will counteract any acceleration I had when I started spinning. That is different than a swipe which will just stop inputting as you lift the finger.
I read that Unity Accelerometer Input also reads data from the Gyroscope on the device; Citation Needed.
Your answer
Follow this Question
Related Questions
Rotate object with accelerometer? 0 Answers
Rotate object to match phone's orientation 1 Answer
Rotating object based on USB Tilt Sensor 0 Answers
Rotate object with rigidbody and high angular drag around particular local axis 0 Answers
Getting X axis input from accelerometer when mobile device is held upright? 4 Answers