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 DroidifyDevs · Feb 01, 2016 at 12:39 AM · androidmobileangletiltdegrees

How to detect the tilt angle of the mobile device on start?

Hi!

Basically, I really need a script that will detect the device tilt angle at the start of the game. Why? Because I'm adding tilt controls to my game, here's the script for that:

 using System;
 using UnityEngine;
 #if UNITY_EDITOR
 using UnityEditor;
 #endif
 
 namespace UnityStandardAssets.CrossPlatformInput
 {
     // helps with managing tilt input on mobile devices
     public class TiltInput : MonoBehaviour
     {
         // options for the various orientations
         public enum AxisOptions
         {
             ForwardAxis,
             SidewaysAxis,
         }
 
 
         [Serializable]
         public class AxisMapping
         {
             public enum MappingType
             {
                 NamedAxis,
                 MousePositionX,
                 MousePositionY,
                 MousePositionZ
             };
 
 
             public MappingType type;
             public string axisName;
         }
 
 
         public AxisMapping mapping;
         public AxisOptions tiltAroundAxis = AxisOptions.ForwardAxis;
         public float fullTiltAngle = 25;
         public float centreAngleOffset = 0;
 
 
         private CrossPlatformInputManager.VirtualAxis m_SteerAxis;
 
 
         private void OnEnable()
         {
             if (mapping.type == AxisMapping.MappingType.NamedAxis)
             {
                 m_SteerAxis = new CrossPlatformInputManager.VirtualAxis(mapping.axisName);
                 CrossPlatformInputManager.RegisterVirtualAxis(m_SteerAxis);
             }
         }
 
 
         private void Update()
         {
             float angle = 0;
             if (Input.acceleration != Vector3.zero)
             {
                 switch (tiltAroundAxis)
                 {
                     case AxisOptions.ForwardAxis:
                         angle = Mathf.Atan2(Input.acceleration.x, -Input.acceleration.y)*Mathf.Rad2Deg +
                                 centreAngleOffset;
                         break;
                     case AxisOptions.SidewaysAxis:
                         angle = Mathf.Atan2(Input.acceleration.z, -Input.acceleration.y)*Mathf.Rad2Deg +
                                 centreAngleOffset;
                         break;
                 }
             }
 
             float axisValue = Mathf.InverseLerp(-fullTiltAngle, fullTiltAngle, angle)*2 - 1;
             switch (mapping.type)
             {
                 case AxisMapping.MappingType.NamedAxis:
                     m_SteerAxis.Update(axisValue);
                     break;
                 case AxisMapping.MappingType.MousePositionX:
                     CrossPlatformInputManager.SetVirtualMousePositionX(axisValue*Screen.width);
                     break;
                 case AxisMapping.MappingType.MousePositionY:
                     CrossPlatformInputManager.SetVirtualMousePositionY(axisValue*Screen.width);
                     break;
                 case AxisMapping.MappingType.MousePositionZ:
                     CrossPlatformInputManager.SetVirtualMousePositionZ(axisValue*Screen.width);
                     break;
             }
         }
 
 
         private void OnDisable()
         {
             m_SteerAxis.Remove();
         }
     }
 }
 
 
 namespace UnityStandardAssets.CrossPlatformInput.Inspector
 {
 #if UNITY_EDITOR
     [CustomPropertyDrawer(typeof (TiltInput.AxisMapping))]
     public class TiltInputAxisStylePropertyDrawer : PropertyDrawer
     {
         public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
         {
             EditorGUI.BeginProperty(position, label, property);
 
             float x = position.x;
             float y = position.y;
             float inspectorWidth = position.width;
 
             // Don't make child fields be indented
             var indent = EditorGUI.indentLevel;
             EditorGUI.indentLevel = 0;
 
             var props = new[] {"type", "axisName"};
             var widths = new[] {.4f, .6f};
             if (property.FindPropertyRelative("type").enumValueIndex > 0)
             {
                 // hide name if not a named axis
                 props = new[] {"type"};
                 widths = new[] {1f};
             }
             const float lineHeight = 18;
             for (int n = 0; n < props.Length; ++n)
             {
                 float w = widths[n]*inspectorWidth;
 
                 // Calculate rects
                 Rect rect = new Rect(x, y, w, lineHeight);
                 x += w;
 
                 EditorGUI.PropertyField(rect, property.FindPropertyRelative(props[n]), GUIContent.none);
             }
 
             // Set indent back to what it was
             EditorGUI.indentLevel = indent;
             EditorGUI.EndProperty();
         }
     }
 #endif
 }

However using centreAngleOffset is very inconvenient as many people will hold their devices at different angles. So, how do I detect the device's tilt angle at the start of the scene, and then use it in place of centreAngleOffset?

THANK YOU!

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

0 Replies

· Add your reply
  • Sort: 

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

49 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

Related Questions

Accelerometer Input Rotation Help 0 Answers

Running game on mobile device results in UI getting positioned weirdly and touches offsetted. 0 Answers

Performance much worse on LWRP than on BuiltIn Pipeline 0 Answers

[Unity5 3D]85 SetPass Calls still running below 15FPS on Galaxy Samsung S3 0 Answers

Is Collider 2D and OnMouseDown a good combination for mobile? 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