Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Drexster · Jun 26, 2012 at 12:08 AM · quaternioneuleranglesgyroscoperotatinggyro

Rotating object within bounds using gyroscope

I've been playing around with the gyroscope for a bit now and am having some trouble achieving the effect i'm after.

I'm trying to rotate an object using the Gyro but within a limited min max rotation (i.e. -90 to 90 on Y and -20 to 20 on the Z for example).

I've tried using the gyro.attitude but it's information is absolute to the devices orientation. So not super usable for this application.

using gyro.rotationRate gives gyro rotation velocity, which i can then add to the object's localEulerAngle. The problem is when the rotation reaches <0 i get popping and clipping. I realize this is due to eulerAngles not accepting -values.

I've tried getting this to work various ways using localRotation and quaternions but my results are worse off.

 var lowerLimitY : float = -40;
 var upperLimitY : float = 40; 
 var lowerLimitX : float = -10;
 var upperLimitX : float = 10;
 var lowerLimitZ : float = -20;
 var upperLimitZ : float = 20;
  
 var newRotation : Vector3;
 var lastRotation : Vector3;
 private var gyroRotRate : Vector3;
 
 function Update () {
     if (gyroBool) {
         
         gyroRotRate = gyro.rotationRate;    
         label.text = "Gyro Rotation Rate = " + gyroRotRate;
         
         //gyro deadzone // sensitivity
         if(gyroRotRate.x > 0.1 || gyroRotRate.y > 0.1 || gyroRotRate.z > 0.1)
             {
                 newRotation = lastRotation + gyroRotRate;
             }
         
         else if(gyroRotRate.x < -0.1 || gyroRotRate.y < -0.1 || gyroRotRate.z < -0.1)
             {
                 newRotation = lastRotation + gyroRotRate;
             }
         label3.text = "New Rotation: " + newRotation;
         label4.text = "Last Rotation: " + lastRotation;
                                                 
     }
     else{label.text = "NO GYRO";
     }
 }
 
 
 function LateUpdate()
 {
 
 if(newRotation.x > upperLimitX) {transform.eulerAngles.x = upperLimitX; }
 else if(newRotation.x < lowerLimitX) {transform.eulerAngles.x = lowerLimitX; }
 else{transform.eulerAngles.x = newRotation.x;}
 
 if(newRotation.y > upperLimitY) {transform.eulerAngles.y = upperLimitY; }
 else if(newRotation.y < lowerLimitY) {transform.eulerAngles.y = lowerLimitY;}
 else{transform.eulerAngles.y = newRotation.y;}
 
 if(newRotation.z > upperLimitZ) {transform.eulerAngles.z = upperLimitZ; }
 else if(newRotation.z < lowerLimitZ) {transform.eulerAngles.z = lowerLimitZ; }
 else{transform.eulerAngles.z = newRotation.z;}
 
 lastRotation = transform.localEulerAngles;
 
 label2.text = "Object Adjusted EulerAngle = " + transform.eulerAngles;
 }


Any ideas/suggestions of what i'm doing wrong?

Comment
Add comment · Show 1
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
avatar image psycohk · Jul 26, 2012 at 02:58 AM 0
Share

I am facing the same problem. I ain't good at maths but I think the problem should be around the angle rotate will take the shortest path.

http://answers.unity3d.com/questions/29505/jerky-rotation-when-using-quaternionslerp-on-rotat.html

Please let me know if this help or not.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by ScroodgeM · Jul 27, 2012 at 03:47 PM

after you got gyro.attitude what can't you multiply with device's base rotation to get rotation you need?

then... limiting rotation speed and angle:

Rotater.cs

using UnityEngine;
public class JustForTestCode : MonoBehaviour
{
    public Vector3 Limits = Vector3.one * 50f;
    public Vector3 Speeds = Vector3.one * 10f;
    public Transform targetRotation;
    Vector3 myRotation = Vector3.zero;
    void Update()
    {
        Vector3 targetEuler = targetRotation.rotation.eulerAngles;
        for (int i = 0; i <= 2; i++)
        {
            myRotation[i] = Mathf.Clamp(Mathf.MoveTowards(myRotation[i], Mathf.Repeat(targetEuler[i] + 180f, 360f) - 180f, Time.deltaTime * Speeds[i]), -Limits[i], Limits[i]);
        }
        transform.rotation = Quaternion.Euler(myRotation);
    }
}
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

6 People are following this question.

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

Related Questions

3rd player orientation using gyro with offset 0 Answers

Rotate with raw gyro data. 0 Answers

3D camera relatively using gyro a la N.O.V.A. 2 0 Answers

How to get the value of the an angle of the Gyroscope? 1 Answer

Quaternion, Eulers and Problems 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