Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Sawula · Jan 16, 2017 at 03:54 PM · androidgravityaccelerometergyroscopetilt

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. alt text

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); 
 
 
     }
 
 }
demo-help.png (259.9 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
 
     }
 
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

136 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Getting linear acceleration not using gyroscope on Android 2 Answers

Tilt scene using accelerometer/gyroscope 0 Answers

Rotate Object by Tilting Android Device Any Angle 1 Answer

calibrating a gyroscope controlled tilt platform(Android) 1 Answer

Move sprite in Y based on phone tilt 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges