Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by Kac26 · Apr 21, 2015 at 06:06 PM · anglegyroscopesteering

Gyroscope steering - set default position of phone

I have a question about gyroscope delault position. I created a game in witch you move space ship foward, back, left, and right by moving the phone(using gyroscope). My problem is that I must hold phone like on picture 1. If i hold the phone like on picture 2, the ship goes down. Anyone know how to change default position of the gyroscope, so I can normally hold the phone and steering the ship? I want the ship stationary if my phone is holded at 45 degrees. Otherwise, it move to the titled direction.

alt text

Picture 1 shows how i hold the phone to have the space ship in the center, and picture 2 shows how i want to hold the phone have the space ship in the center.

Anybody know how to do that?

My code for steering the ship:

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class Boundaryy
 {
     public float xMin, xMax, zMin, zMax;
 }
 
 public class PlayerControll1 : MonoBehaviour {
     public float speed;
     public float tilt;
     public Boundaryy boundary;
     
     void Start () {
         Input.gyro.enabled = true;
         Screen.sleepTimeout = SleepTimeout.NeverSleep;
          }
 
      void FixedUpdate() {
 
         float xi = Input.gyro.userAcceleration.x;
         float yi = Input.gyro.userAcceleration.y;
 
 
         Vector3 movement = new Vector3 (xi, 0.0f, yi);
         
         movement = Quaternion.AngleAxis(45, Vector3.right) * movement;
 
 
              rigidbody.velocity = movement * speed;
         
         rigidbody.position = new Vector3 
             (
                 Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax), 0.0f, 
                 Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
                 );
         
         rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
 
     }    
 }

39102-zajetaslika.jpg (29.4 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

Answer by campeanionut93 · Jan 15, 2019 at 03:53 PM

I know this is an old question, but maybe somebody else stumbles upon this.

You might want to look into Input.gyro.gravity. That returns a Vector3 with the real world gravity that is applied to each axis, in the range [0, 1] (where 0 is no gravity i.e. the axis is perpendicular on the gravity vector, and 1 is full gravity i.e. the axis is aligned with the earth's gravity vector).

For example, if you hold your phone like in picture 1, the gravity on the Y axis (top to bottom of your phone, depending on orientation) would be 0. In picture 2, assuming you hold your phone at a 45 degree angle, the gravity value for Y would be sine(45 deg) ~= 0.7.

If we apply some simple trigonometry (search google for the trigonometric circle and sine function), we basically want to offset the graph of the sine function. Since we know that Input.gyro.gravity.y is the sine of the y axis angle relative to the gravity vector, we can do this:

 float NormalizeGravity(float axisValue, float defaultRotationInDegrees)
 {
     return Mathf.Sin(Mathf.Asin(axisValue) + Mathf.Deg2Rad * defaultRotationInDegrees);
 }

DISCLAIMER: I tested this in landscape mode. You may also need to take care of the sign of the Sin and Asin functions.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Steering Wheel with mouse drag 0 Answers

Get Gyro 3-Axis Angle of Smartphone in Unity 0 Answers

2d Raycasting, trouble drawing rays at the correct angles 1 Answer

Steering Overcompesation 0 Answers

How to calculate angle in the triangle formed by screen touches 1 Answer


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