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 ganesh-mune · Feb 06, 2014 at 01:15 PM · rotationlimitgyro

Trying to use Gyro to see the scene in unity but can't limit the camera

Hi friends, I got script for the gyro, and that script work well and the camera rotates in all directions but I want to limit it so that I can restrict the view. And I am able to restrict in one direction only same way tried to restrict in other direction but its not working :(

Here is the code // *********************************************************** // Written by Heyworks Unity Studio http://unity.heyworks.com/ // *********************************************************** using UnityEngine; /// /// Gyroscope controller that works with any device orientation. /// public class GyroController : MonoBehaviour { #region [Private fields] private bool gyroEnabled = true; private const float lowPassFilterFactor = 0.2f; private readonly Quaternion baseIdentity = Quaternion.Euler(90, 0, 0); private readonly Quaternion landscapeRight = Quaternion.Euler(0, 0, 180); private readonly Quaternion landscapeLeft = Quaternion.Euler(0, 0, -180); private readonly Quaternion upsideDown = Quaternion.Euler(0, 0, 180); private Quaternion cameraBase = Quaternion.identity; private Quaternion calibration = Quaternion.identity; private Quaternion baseOrientation = Quaternion.Euler(90, 0, 0); private Quaternion baseOrientationRotationFix = Quaternion.identity; private Quaternion referanceRotation = Quaternion.identity; private bool debug = true; public GameObject niddle; public Quaternion tempRot; public float minVal; public float maxVal; //public GUIText text; //public TextMesh Text3D; #endregion #region [Unity events] void Start () { AttachGyro(); minVal = 0.64f; maxVal = 0.1f; Input.gyro.enabled = true; } protected void Update() { if (!gyroEnabled) return; tempRot = Quaternion.Slerp(transform.rotation, cameraBase * ( ConvertRotation(referanceRotation * Input.gyro.attitude) * GetRotFix()), lowPassFilterFactor); /* Extra lines added for the limitation by me */ tempRot.y = Mathf.Clamp(tempRot.y,70.0f,191.0f); tempRot.z = Mathf.Clamp(tempRot.z,0.0f,0.0f); tempRot.x = Mathf.Clamp(tempRot.x,0.0f,0.0f); /* ======================== */ transform.rotation = tempRot; float minusfactor = 90.0f; float zRotation = transform.rotation.z; zRotation = minusfactor; niddle.gameObject.transform.rotation = tempRot; } protected void OnGUI() { if (!debug) return; GUILayout.Label("Orientation: " + Screen.orientation); GUILayout.Label("Calibration: " + calibration); GUILayout.Label("Camera base: " + cameraBase); GUILayout.Label("input.gyro.attitude: " + Input.gyro.attitude); GUILayout.Label("transform.rotation: " + transform.rotation); if (GUILayout.Button("On/off gyro: " + Input.gyro.enabled, GUILayout.Height(100))) { Input.gyro.enabled = !Input.gyro.enabled; } if (GUILayout.Button("On/off gyro controller: " + gyroEnabled, GUILayout.Height(100))) { if (gyroEnabled) { DetachGyro(); } else { AttachGyro(); } } if (GUILayout.Button("Update gyro calibration (Horizontal only)", GUILayout.Height(80))) { UpdateCalibration(true); } if (GUILayout.Button("Update camera base rotation (Horizontal only)", GUILayout.Height(80))) { UpdateCameraBaseRotation(true); } if (GUILayout.Button("Reset base orientation", GUILayout.Height(80))) { ResetBaseOrientation(); } if (GUILayout.Button("Reset camera rotation", GUILayout.Height(80))) { transform.rotation = Quaternion.identity; } } #endregion #region [Public methods] /// <summary> /// Attaches gyro controller to the transform. /// </summary> private void AttachGyro() { gyroEnabled = true; ResetBaseOrientation(); UpdateCalibration(true); UpdateCameraBaseRotation(true); RecalculateReferenceRotation(); } /// <summary> /// Detaches gyro controller from the transform /// </summary> private void DetachGyro() { gyroEnabled = false; } #endregion #region [Private methods] /// <summary> /// Update the gyro calibration. /// </summary> private void UpdateCalibration(bool onlyHorizontal) { if (onlyHorizontal) { var fw = (Input.gyro.attitude) * (-Vector3.forward); fw.z = 0; if (fw == Vector3.zero) { calibration = Quaternion.identity; } else { calibration = (Quaternion.FromToRotation(baseOrientationRotationFix * Vector3.up, fw)); } } else { calibration = Input.gyro.attitude; } } /// <summary> /// Update the camera base rotation. /// </summary> /// <param name='onlyHorizontal'> /// Only y rotation. /// </param> private void UpdateCameraBaseRotation(bool onlyHorizontal) { if (onlyHorizontal) { var fw = transform.forward; fw.y = 0; if (fw == Vector3.zero) { cameraBase = Quaternion.identity; } else { cameraBase = Quaternion.FromToRotation(Vector3.forward, fw); } } else { cameraBase = transform.rotation; } } /// <summary> /// Converts the rotation from right handed to left handed. /// </summary> /// <returns> /// The result rotation. /// </returns> /// <param name='q'> /// The rotation to convert. /// </param> private static Quaternion ConvertRotation(Quaternion q) { return new Quaternion(q.x, q.y, -q.z, -q.w); } /// <summary> /// Gets the rot fix for different orientations. /// </summary> /// <returns> /// The rot fix. /// </returns> private Quaternion GetRotFix() { #if UNITY_3_5 if (Screen.orientation == ScreenOrientation.Portrait) return Quaternion.identity; if (Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.Landscape) return landscapeLeft; if (Screen.orientation == ScreenOrientation.LandscapeRight) return landscapeRight; if (Screen.orientation == ScreenOrientation.PortraitUpsideDown) return upsideDown; return Quaternion.identity; #else return Quaternion.identity; #endif } /// <summary> /// Recalculates reference system. /// </summary> private void ResetBaseOrientation() { baseOrientationRotationFix = GetRotFix(); baseOrientation = baseOrientationRotationFix * baseIdentity; } /// <summary> /// Recalculates reference rotation. /// </summary> private void RecalculateReferenceRotation() { referanceRotation = Quaternion.Inverse(baseOrientation)*Quaternion.Inverse(calibration); } #endregion }

Please help me to solve the problem :( Thanks in advance...

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

18 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

Related Questions

Manually computing rotation constrain limits 0 Answers

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

c# problem with limiting rotation (with rigidbody) 1 Answer

Limiting rotations to box edge 1 Answer

LookAt Limit Up and Down rotation only 2 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