Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Nov 10, 2017 at 11:01 PM by Patomanriquezb for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Patomanriquezb · Nov 10, 2017 at 10:26 PM · camerarotationquaterniondrageulerangles

Quaternion - Euler Angles, weird variable swapping when converting

Hi everyone. I'm making this android game and I want to move my player camera by dragging across the screen. I got a really useful script for it, but I'm having a little problem that bugs me and I can't solve. I've been looking for hours for the solution, but I can't find it. So, here's the deal: I get the difference between my initial mouse position when I click (or tap) the screen and then I get the difference to the new mouse position. After that, I apply the rotation. Everything works fine, BUT, if I click (and hold) on the same spot twice, the camera jumps to a new rotation the first time and backwards the second time. I checked the rotation Quaternion and the X and Y variables swap, making this weird effect. I don't know why it is happening, so If any of you can shed some light on the matter I'd be really thankful.

CODE:

     private bool bDragging;
     private Vector3 oldPos;
     private Vector3 panOrigin;
     public float panSpeed;
     public Vector3 pos;
     public Quaternion auxQuaternion;
 
 
     private float clickTimerDefault = 0.5f;
     private float clickTimer;
 
     private Camera firstPCamera;
 
     new void Start()
     {
         base.Start();
         firstPCamera = GameObject.Find("FirstPersonCamera").GetComponent<Camera>();
     }
     // Update is called once per frame
     void Update () {
 
         if (Input.GetMouseButtonDown(0))
         {
             bDragging = true;
             oldPos = firstPCamera.transform.rotation.eulerAngles;
             panOrigin = firstPCamera.ScreenToViewportPoint(Input.mousePosition);                    //Get the ScreenVector the mouse clicked
             clickTimer = clickTimerDefault;
         }
         if (bDragging && clickTimer > 0)
         {
             clickTimer -= Time.deltaTime;
         }
 
         if (Input.GetMouseButton(0) && clickTimer <= 0)
         {
             pos = firstPCamera.ScreenToViewportPoint(Input.mousePosition) - panOrigin;    //Get the difference between where the mouse clicked and where it moved
             Vector3 aux = oldPos + pos * panSpeed;
             aux = new Vector3(-aux.y, -aux.x, 0); //Thi conversion was needed to adjust the axis
             auxQuaternion = Quaternion.Euler(aux);
             firstPCamera.transform.rotation = auxQuaternion;
         }
 
         if (Input.GetMouseButtonUp(0))
         {
             bDragging = false;
         }
     }



First click alt text

Second click alt text

Please somebody help me :c

Thanks.

screenshot-156.png (14.2 kB)
screenshot-157.png (14.3 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

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by Patomanriquezb · Nov 10, 2017 at 11:00 PM

I solved it, I was converting the wrong vector. Solution:

         if (Input.GetMouseButton(0) && clickTimer <= 0)
         {
             pos = firstPCamera.ScreenToViewportPoint(Input.mousePosition) - panOrigin;    //Get the difference between where the mouse clicked and where it moved
             Vector3 aux = new Vector3(-pos.y, -pos.x, 0); //This conversion was needed to adjust the axis
             aux = oldPos + aux * panSpeed;
             auxQuaternion = Quaternion.Euler(aux);
             firstPCamera.transform.rotation = auxQuaternion;
         }
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 Bunny83 · Nov 10, 2017 at 10:47 PM

You ignore the z rotation which might be the cause of your problem. Keep in mind that eulerangles is not a vector in the sense of a direction. It's just a collection of 3 seperate angles. Quaternion to eulerangles conversion is not unique. You can simply flip all 3 angles by 180° and get the same rotation. Imagine you look just forward. If you rotate 180° on y you look in the opposite direction. If you now rotate 180° around x you doing a half looping so you look again forward but your view is upside down. Finally rotate by 180° around z and you get the same rotation as in the beginning.


If you only control your camera with this mechanic, there's no need to read back the current rotation. Just save your last used eulerangles

Comment
Add comment · Show 1 · 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 Patomanriquezb · Nov 10, 2017 at 11:02 PM 0
Share

Thanks anyway!

Follow this Question

Answers Answers and Comments

132 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

allows users to view 360º content without a VR headset 0 Answers

How to make camera rotation relative to target. 3 Answers

Edited mouselook script rotation clamp not working 0 Answers

Camera.WorldToScreenPoint Equivalent 1 Answer

How to rotate an object around another 60 degrees with a keypress? 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