This site uses strictly necessary cookies. More Information

X# Gravity on Android

Hello folks, I need help for something. I got an android game in which gravity change depending how you tilt the phone. I managed to get it to tilt forward, backward, and to the side. But I can't get it to reverse.

As shown in the image below, when I turn the phone following the arrows, I want the gravity to slowly go in reverse. So we go from the up left picture, to the right down picture.

I tried with the accelerometer, I tried with the gyroscope, but so far I lucked out. Anyone can help? Here is my code so far

```
using UnityEngine;
using System.Collections;
public class GravityGyro : MonoBehaviour {
Vector3 grav_ini;
float tempy;
float tempx;
float tempz;
public float _speed;
public int UpDown = 0;
void Awake () {
Input.gyro.enabled = true;
grav_ini = Physics.gravity;
tempy = 0 - (float)System.Math.Round(Input.acceleration.y,1);
tempx = 0 - (float)System.Math.Round(Input.acceleration.x,1);
tempz = 0 - (float)System.Math.Round(Input.acceleration.z,1);
}
public void ResetGravity(){
//I have a button to reset gravity should I need it
tempy = 0 - (float)System.Math.Round(Input.acceleration.y,1);
tempx = 0 - (float)System.Math.Round(Input.acceleration.x,1);
tempz = 0 - (float)System.Math.Round(Input.acceleration.z,1);
Input.gyro.enabled = true;
}
// Update is called once per frame
void Update () {
//gravity tilt
// I tested the axis one by one to see which one I needed to use where, except for the one that command Physics.gravity.y which is the one I'm stuck on
Physics.gravity = grav_ini + (new Vector3((float)System.Math.Round(tempx + Input.acceleration.x,1),(float)System.Math.Round(tempy + Input.acceleration.y,1),(float)System.Math.Round(tempy + Input.acceleration.y,1))*_speed);
}
}
```

**Answer** by Sawula
·
Jan 18, 2017 at 01:14 PM

nevermind, a friend at school helped me

```
using UnityEngine;
using System.Collections;
public class GravityGyro : MonoBehaviour {
Vector3 grav_ini;
float tempy;
float tempx;
float tempz;
float gravgyroup;
public float _toPlafond;
public float _speed;
void Awake () {
Input.gyro.enabled = true;
Physics.gravity = new Vector3 (0,-150,0);
grav_ini = Physics.gravity;
tempy = 0 - (float)System.Math.Round(Input.acceleration.y,1);
tempx = 0 - (float)System.Math.Round(Input.acceleration.x,1);
gravgyroup = 0;
}
public void ResetGravity(){
Input.gyro.enabled = true;
Physics.gravity = new Vector3 (0,-150,0);
grav_ini = Physics.gravity;
tempy = 0 - (float)System.Math.Round(Input.acceleration.y,1);
tempx = 0 - (float)System.Math.Round(Input.acceleration.x,1);
gravgyroup = 0;
}
// Update is called once per frame
void Update () {
Debug.Log (Physics.gravity.y);
//gravity tilt
Physics.gravity = grav_ini + (new Vector3((float)System.Math.Round(tempx + Input.acceleration.x,1),(gravgyroup - grav_ini.y)*_toPlafond/_speed,(float)System.Math.Round(tempy + Input.acceleration.y,1))*_speed);
gravgyroup = ((600/360) * (float)System.Math.Round(Input.gyro.attitude.eulerAngles.z)) -300;
}
}
```

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.