# Unity3D - How to get highest value for acceleration from Input.gyro

I'm trying to develop an app that can detect car acceleration, braking and cornering speed. I need to detect device acceleration. Is that acceleration high or low?. Like flo app on Google Play.

I'm using gyroscope and I need to get highest value for user acceleration (x, y, z axises) from gyroscope. Values of x, y and z are changing every frame. I need to achieve highest value of this 3 axis for later use.

Now my current code looks like this

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class NewBehaviourScript : MonoBehaviour
{
public Text x, y, z;

void Start()
{
Input.gyro.enabled = true;
}

void Update()
{
x.text = Mathf.Abs(Input.gyro.userAcceleration.x).ToString();
y.text = Mathf.Abs(Input.gyro.userAcceleration.y).ToString();
z.text = Mathf.Abs(Input.gyro.userAcceleration.z).ToString();
}
}


Thanks for any help

You can store your Input.gyro.userAcceleration into a Vector3 variable to make your life easier in your Update().

Then you will need to store the maximum value into some class scoped variables, so I would start writing this in your class:

public float maxX = 0f, maxY = 0f, maxZ = 0f, maxMagnitude = 0f;


And update those values if you get an higher value:

void Update()
{
Vector3 acceleration = Input.gyro.userAcceleration;

x.text = Mathf.Abs(acceleration.x).ToString();
y.text = Mathf.Abs(acceleration.y).ToString();
z.text = Mathf.Abs(acceleration.z).ToString();

if (maxX < Mathf.Abs(acceleration.x)) {
maxX = Mathf.Abs(acceleration.x);
}

if (maxY < Mathf.Abs(acceleration.y)) {
maxY = Mathf.Abs(acceleration.y);
}

if (maxZ < Mathf.Abs(acceleration.z)) {
maxZ = Mathf.Abs(acceleration.z);
}

if (maxMagnitude < acceleration.magnitude) {
maxMagnitude = acceleration.magnitude;
}
}


EDITED after update to fit the question edits.

• Thanks for trying to help. But this not what I wanted :) I'm trying to develop an app that can detect car acceleration, braking and cornering speed. I need to detect device acceleration. Is that acceleration high or low?. Like flo app on Google Play – Nurlan Shukurov Apr 7 '16 at 6:33
• Sorry, my mistake, but by "highest value for user acceleration from gyroscope" that's what I understood. Please clarify this in your question so we would be able to answer properly. To say if an acceleration value is high or low, it only depends on your criteria. Cornering at 0.7 G is pretty high for a family sedan, but Formula 1 cars can turn corners at 5 G. You must set a threshold or let the users set up their own. – JordiVilaplana Apr 7 '16 at 6:44
• No, this is my mistake when asked non-clear question. And this my job to determine criteria. I'm editing question – Nurlan Shukurov Apr 7 '16 at 6:55