Search Unity

Quaternion.AngleAxis shows unexpected behaviour

Discussion in 'Physics' started by zainnn, Jun 17, 2019.

  1. zainnn

    zainnn

    Joined:
    May 6, 2019
    Posts:
    10
    I am rotating two circles in a game with mouse drag. Circle 1 rotates normally and circle 2 is expected to move opposite and slower. The problem occurs with Circle 2 only when i use Quaternion.AngleAxis and use a fraction factor to multiply with the angle used for circle 1.

    Here is my code:
    Code (CSharp):
    1. using UnityEngine;
    2. public GameObject Circle1, Circle2;
    3.  
    4. void OnMouseDown()
    5.     {
    6.         Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
    7.         pos = Camera.main.WorldToScreenPoint(transform.position);
    8.         pos = Input.mousePosition - pos;
    9.         baseAngle = Mathf.Atan2(pos.y, pos.x) * Mathf.Rad2Deg;
    10.         baseAngle -= Mathf.Atan2(transform.right.y, transform.right.x) * Mathf.Rad2Deg;
    11.     }
    12.  
    13.     void OnMouseDrag()
    14.     {
    15.         Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
    16.         pos = Input.mousePosition - pos;
    17.         float ang = Mathf.Atan2(pos.y, pos.x) * Mathf.Rad2Deg - baseAngle;
    18.         Circles1.transform.rotation = Quaternion.AngleAxis(ang, Vector3.forward);
    19.         float sp = 0.2f;
    20.         Circle2.transform.rotation = Quaternion.Inverse(ang * sp, Vector3.forward);
    21. }
    22.  
    P.S with or without this baseAngle i used in code, there was no difference.
     
    Last edited: Jun 17, 2019