I would like to set the world gravity based on the Gyroscope, so if someone rotates their device the gravity changes as well.
I assume this is a fairly easy task, but I am not sure how to do it, and I can’t seem to find anything on Google about it either.
Any help on this is appreciated!
I have never used the accelometer but I am sure, you could relate a tilt angle to an applied force.
I am not sure what you mean… could you please explain? Thanks!
I would think it would be something like this…
Physics2D.gravity = Input.gyro.attitude.eulerAngles;
Something like that. You would have to test.
From what I am reading, it looks like have to use Input.acceleration to get the rotation. But still don’t know how to set gravity…
There we go! I got it working!
public class Main : MonoBehaviour {
// Update is called once per frame
void FixedUpdate(){
if(Application.platform == RuntimePlatform.Android){
float gx = Input.acceleration.x * 9.81f;
float gy = Input.acceleration.y * 9.81f;
Physics2D.gravity = new Vector2(gx, gy);
}
}
}
1 Like