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
6
Question by MedievalPete · May 20, 2015 at 03:58 PM · androidcameracamera rotategyroscope

Rotating a camera using the gyroscope

Hi all. I've been trying for the past few days to implement a simple(!) piece of code which would allow me to control the horizontal rotation of a camera using the gyroscope of my android device. It is a first person game, in which the player should be able to 'look around' 360 degrees along the horizontal axis. Imagine sitting in an office chair with the device held out in front of you, then spinning round 360 degrees. As you spin, so should the camera in the game. Here's the closest I've come:

 using UnityEngine;
 using System.Collections;
 public class GyroRotate : MonoBehaviour {
     
     void Start () 
     {
         Input.gyro.enabled = true;
     }
 
     void Update () 
     {
         var x = Input.gyro.rotationRateUnbiased.x;
         transform.eulerAngles = new Vector3 (x, 0, 0);
     }
 }

This script is attached to an empty game object with the main camera as a child. The problem with this is the camera only rotates a little, then always snaps back to its original position. Any help or advice would be hugely appreciated - I'm quite new to all of this!

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

4 Replies

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

Answer by MedievalPete · May 21, 2015 at 01:24 PM

I solved it - in case anyone else was curious, this was the code I needed:

 void Update () 
     {
         player.transform.Rotate (0, -Input.gyro.rotationRateUnbiased.y, 0);
     }
Comment
Add comment · Show 7 · 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
avatar image Entity007 · Jun 28, 2016 at 07:57 PM 1
Share

Thank you for giving answer!

avatar image RichyK Entity007 · Sep 21, 2016 at 12:11 AM 1
Share

Thank you! I was thinking about using the gyroscope in my new game, and it all it took is your one line!!

avatar image fallengamer · Nov 21, 2016 at 03:16 PM 0
Share

I want to know for both axis horizental and vertical

avatar image AJ01 · Jan 03, 2017 at 10:03 PM 1
Share

I've used this code and works excellently except that the camera moves too quickly in-game, is there any way to slow down how fast this moves the camera?

avatar image joonatri AJ01 · Jan 14, 2019 at 02:39 PM 0
Share

Just do this. It works for me

Thank you @$$anonymous$$edievalPete for the answer!

 void Update ()
 {
       player.transform.Rotate (0, -Input.gyro.rotationRateUnbiased.y/2, 0);
 }
avatar image ShaileshM · Jun 15, 2017 at 01:24 PM 0
Share

Thanks, it works like a charm (y) I have modify the code according to my requirement float sens = 50.0f; // to control the speed of camera rotaion void Update() {
transform.Rotate(0, -Input.gyro.rotationRateUnbiased.y sens Time.deltaTime, 0); }

avatar image Content1of_akind · May 17, 2019 at 07:26 AM 0
Share

Appreciate it! =)

avatar image
0

Answer by fallengamer · Nov 21, 2016 at 03:25 PM

I want to know how I can integrate for the both axis of gyro ?

Comment
Add comment · Show 4 · 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
avatar image Entity007 · Nov 22, 2016 at 09:04 AM 0
Share

Hello

By using the same logic you can add


void Update () { player.transform.Rotate (-input.gyro.rotationRateUnbiased.x, -Input.gyro.rotationRateUnbiased.y, 0); }


avatar image zero_null · Nov 23, 2016 at 07:59 AM 2
Share

Is this really an answer ? :/

avatar image AJ01 · Jan 02, 2017 at 09:03 PM 0
Share

This works really well, but it moves the camera way too fast and is very jittery, is there a way to slow down the input?

avatar image DrBraulio · May 11, 2017 at 07:59 AM 0
Share

If you want a rotation whit more flow take a look on Quaternion.Slerpt and if you want to apply less rotation just use a variable to divide the parameter of your input.

avatar image
0

Answer by makaka-org · May 17, 2019 at 08:44 AM

Hi, Guys! I used technique of camera rotating with Gyro in my AR Cameras:

1. AR Camera GYRO;
2. AR + VR: Mixed Reality.

Asset Documentation.

alt text


ar-vr-mr-mixed-reality-insta.png (224.3 kB)
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
avatar image
0

Answer by bionicartsmobile · Jun 10, 2020 at 04:00 AM

I love the simplicity of this rotation, but there's a minor issue. Can you help?

So, using the following code:

 void Update()
     {
         transform.Rotate(-Input.gyro.rotationRateUnbiased.x, -Input.gyro.rotationRateUnbiased.y, Input.gyro.rotationRateUnbiased.z);
     }


The Z-Axis tends to get offset after moving my device after a few seconds. For example: If I rotate my device counter-clockwise, then the environment tends to rotate clockwise in the Z-Axis and vice versa, if I rotate my phone clockwise then the environment offsets counter-clockwise.


alt text alt text


Is there a way to fix this? A way to correct the unwanted Z-Rotation? (Note: even if I set the Z-Axis to Zero (0) it still has this unwanted rotation)


zrotation2.png (436.0 kB)
zrotation.png (370.9 kB)
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

15 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

Related Questions

Gyroscope Orbit Android 1 Answer

Android gyroscope 4 Answers

How to set viewpoint regardless of tilt of device when use gyroscope 0 Answers

my camera show an extremely weird aspect [android] 0 Answers

Gyroscope smooth transitions (minimizing jerk from tiny movements) 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