Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 octabond · Nov 13, 2016 at 10:20 PM · touchcamera-movementcamera rotategyroscope

Camera rotation with gyroscope + drag/swipe

I want to emulate the effect of 360 images where you can rotate in your mobile device with the gyroscope but also with the users touch input.

I have two scripts working separately but when I attached them to the camera they have problems. I think this is because of the fact that the gyro movement script is checking the rotation relative to the device position on each update and its not allowing the drag input to change the view.

Portion for moving with user swipe:

 else if (touch.phase == TouchPhase.Moved)
             {
                 //swiping
                 float deltaX = initTouch.position.x - touch.position.x;
                 float deltaY = initTouch.position.y - touch.position.y;
                 rotX -= deltaY * Time.deltaTime * rotSpeed * dir;
                 rotY += deltaX * Time.deltaTime * rotSpeed * dir;
                 Mathf.Clamp(rotX, -45f, 45f);
                 cam.transform.eulerAngles = new Vector3(rotX, rotY, 0f);                
             }

Portion of the other script for gyro movement

 protected void Update() 
     {
         if (!gyroEnabled)
             return;
         transform.rotation = Quaternion.Slerp(transform.rotation,
             cameraBase * ( ConvertRotation(referanceRotation * Input.gyro.attitude) * GetRotFix()), lowPassFilterFactor);
     }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by danieldourado_2 · Mar 17, 2019 at 03:04 AM

It can be done using one script to turn the camera using touch, and another script to turn the camera using the gyroscope.

BasicCameraRotation .cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BasicCameraRotation : MonoBehaviour
 {
     public void RotateUpDown(float axis)
     {
         transform.RotateAround(transform.position, transform.right, -axis * Time.deltaTime);
     }
 
     //rotate the camera rigt and left (y rotation)
     public void RotateRightLeft(float axis)
     {
         transform.RotateAround(transform.position, Vector3.up, -axis * Time.deltaTime);
     }
 }

GyroscopeCameraRotation.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GyroscopeCameraRotation : BasicCameraRotation
 {
     private float x;
     private float y;
 
     public bool gyroEnabled = false;
     readonly float sensitivity = 50.0f;
 
     private Gyroscope gyro;
 
     void Start()
     {
         gyroEnabled = EnableGyro();
     }
 
     private bool EnableGyro()
     {
         if (SystemInfo.supportsGyroscope)
         {
             gyro = Input.gyro;
             gyro.enabled = true;
             return true;
         }
 
         return false;
     }
     void Update()
     {
         if (gyroEnabled)
         {
             GyroRotation();
         }
     }
     void GyroRotation()
     {
         x = Input.gyro.rotationRate.x;
         y = Input.gyro.rotationRate.y;
 
         float xFiltered = FilterGyroValues(x);
         RotateUpDown(xFiltered*sensitivity);
 
         float yFiltered = FilterGyroValues(y);
         RotateRightLeft(yFiltered * sensitivity);
     }
 
     float FilterGyroValues(float axis)
     {
         if (axis < -0.1 || axis > 0.1)
         {
             return axis;
         }
         else
         {
             return 0;
         }
     }
 }

TouchCameraRotation.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TouchCameraRotation : BasicCameraRotation
 {
     Vector3 firstPoint;
     float sensitivity = 2.5f;
 
     void Update()
     {
        TouchRotation();
     }
     void TouchRotation()
     {
         if (Input.touchCount > 0)
         {
             if (Input.GetTouch(0).phase == TouchPhase.Began)
             {
                 firstPoint = Input.GetTouch(0).position;
             }
             if (Input.GetTouch(0).phase == TouchPhase.Moved)
             {
                 Vector3 secondPoint = Input.GetTouch(0).position;
 
                 float x = FilterGyroValues(secondPoint.x - firstPoint.x);
                 RotateRightLeft(x * sensitivity);
 
                 float y = FilterGyroValues(secondPoint.y - firstPoint.y);
                 RotateUpDown(y * -sensitivity);
 
                 firstPoint = secondPoint;
             }
         }
     }
     float FilterGyroValues(float axis)
     {
         float thresshold = 0.5f;
         if (axis < -thresshold || axis > thresshold)
         {
             return axis;
         }
         else
         {
             return 0;
         }
     }
 }

Here is a sample project with the solution: https://github.com/danieldourado/gyroscope-touch-camera-rotation

Comment
Add comment · Show 2 · 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 cdr9042 · Nov 27, 2019 at 05:02 AM 0
Share

is it possible to add gyro rotation on the Z axis too?

avatar image SWISH93 · Apr 14 at 04:51 AM 0
Share

Hi, my touch rotation script looks a bit differently from the one you have, I have been trying to figure it out but the absolute value vs differential value is confusing as I am using quaternions. This is what my script looks like, Can you advise?

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class MouseLook : MonoBehaviour { public float mouseSensitivity = 100f; public Transform playerBody; float xRotation; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { float mouseX = 0; float mouseY = 0; if(Input.touchCount >0 && Input.GetTouch(0).phase == TouchPhase.Moved) { if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)) return; mouseX = Input.GetTouch(0).deltaPosition.x; mouseY = Input.GetTouch(0).deltaPosition.y; } mouseX *= mouseSensitivity; mouseY *= mouseSensitivity; xRotation -= mouseY * Time.deltaTime; xRotation = Mathf.Clamp(xRotation, -80, 80); transform.localRotation = Quaternion.Euler(xRotation, 0, 0); playerBody.Rotate(Vector3.up * mouseX * Time.deltaTime); } }

avatar image
0

Answer by VRARAPPS · Sep 20, 2017 at 05:32 PM

Have u got the answer ?

Comment
Add comment · Show 3 · 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 ImDev · Oct 27, 2017 at 07:09 PM 0
Share

have u got solution for that?. i am also doing that king of stuff please help me if u know

avatar image VRARAPPS · Oct 31, 2017 at 01:17 PM 0
Share

@ImDev See If its working..

 private Vector3 mouseOrigin;
 private bool isRotating;
 void Update () 
     {
         if(Input.Get$$anonymous$$ouseButtonDown(0))
         {
             // Get mouse origin
             mouseOrigin = Input.mousePosition;
             isRotating = true;
         }
                if (!Input.Get$$anonymous$$ouseButton(0)) isRotating=false;
                if (isRotating)
         {
             Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);
 
             //transform.RotateAround(transform.position, transform.right, -pos.y * turnSpeed); //around x
             transform.RotateAround(transform.position, Vector3.up, pos.x * turnSpeed); // around y
         }
 }
avatar image GuardFromUA VRARAPPS · Mar 07, 2018 at 09:00 AM 0
Share

and do you have gyro and swipe part?

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

9 People are following this question.

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

Related Questions

Allow both turning of the mouse and turning of the player to keep the camera pointing towards the player 1 Answer

How can I make the camera rotate with gyroscope appropriately? 1 Answer

Free Orbit Cam and Mouse Aim Cam aren't working together 0 Answers

Screen is 2 cameras with each different properties. 1 Answer

How to make camera mouse orbit not jittery? 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