Search Unity

360 degrees check rotation

Discussion in 'Scripting' started by freelion93, Aug 19, 2017.

  1. freelion93

    freelion93

    Joined:
    Mar 2, 2015
    Posts:
    13
    Ok so the main idea is that if object camera coliding with one of "waypoints" then it start to check camera's Y angle. Then appear message that player need to turn himself 360' and if he done it the message gone. Problem is that how to check that he made it +360.
    Also in the moment of collide rotate also multiplied by 3 (rotCoef).

    Code (CSharp):
    1. void setNewData()
    2.     {
    3.         Camera cam = GetComponent<Camera>();
    4.         cam.SetStereoViewMatrix(Camera.StereoscopicEye.Left, cam.worldToCameraMatrix);
    5.         cam.SetStereoViewMatrix(Camera.StereoscopicEye.Right, cam.worldToCameraMatrix);
    6.  
    7.         Player1.transform.localPosition = getPosition();
    8.         Player1.transform.localRotation = getRotation();
    9.  
    10.         angleSoFar = 0;
    11.  
    12.         if (colideme == true)
    13.         {
    14.             rotCoef = 3.001f;
    15.             spinerMSG = true;
    16.             angleSoFar = cam.transform.localEulerAngles.y;
    17.         }
    18.        
    19.         if (angleSoFar > collisionangle)
    20.         {
    21.             deactivator();
    22.         }
    23.  
    24.     }
    25.  
    26. private void deactivator()
    27.     {
    28.         if (angleSoFar > coefka)
    29.         {
    30.             colideme = false;
    31.             spinerMSG = false;
    32.             angleSoFar = 0;
    33.             collisionangle = 0;
    34.             rotCoef = 1f;
    35.         }
    36.     }
    37.  
    38.     private void OnGUI()
    39.     {
    40.         if (spinerMSG == true)
    41.         {
    42.             GUI.Label(new Rect(0, 0, 100, 50), "SPIN 360 Degrees!");
    43.         }
    44.     }
    45. private void OnCollisionEnter(Collision col)
    46.     {
    47.         Camera cam = GetComponent<Camera>();
    48.         if (col.gameObject.tag == "waypoint")
    49.         {
    50.             collisionangle = cam.transform.localEulerAngles.y;
    51.             if (collisionangle < 3.0949f || collisionangle >= 0f)
    52.             {
    53.                 coefka = collisionangle + 356.905f;
    54.             }
    55.             if (collisionangle >= 3.0949f)
    56.             {
    57.                 coefka = (collisionangle + 356.905f) + 360f;
    58.             }
    59.          Debug.Log("Colided");
    60.          Debug.Log(collisionangle);
    61.          colideme = true;
    62.         }
    63.     }
    64.     private void OnCollisionExit(Collision collision)
    65.     {
    66.        
    67.         if (collision.gameObject.tag == "waypoint")
    68.         {
    69.             GameObject temp = waypoints.transform.GetChild(0).gameObject;
    70.         foreach (Transform child in waypoints.transform)
    71.         {
    72.             Destroy(temp);
    73.         }
    74.         }
    75.     }
     
  2. freelion93

    freelion93

    Joined:
    Mar 2, 2015
    Posts:
    13
    Anyone?
     
  3. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    916
    Quaternions themselves (in general not just Unity) typically just track the current direction. localEulerAngles won't help you here because they're actually just a conversion to and from the Quaternion, thus never will be greater than 360. things like rotational deltas and rotational velocities are typically tracked in parallel and applied to the quaternion on that frame.

    Take the props on a helicopter for example. the quaternion used to rotate the props only tracks the current orientation, it doesn't save how many spins it's made over the lifetime of the helo. Under most circumstances it'd simply be a waste of data to track that, and likely would suffer from precision loss. Instead some script/animation is likely just tracking how fast the props should spin and then applies an addition/set on the current quaternion for that frame.

    Instead of tracking the camera's transform rotation itself, why not just track the same inputs and rigidbodies that are typically affecting the camera's rotation and add up all the deltas into a private variable. once the absolute value of that delta breaks a threshold (absolute to allow for spinning left or right) then the character has met the objective.
     
  4. freelion93

    freelion93

    Joined:
    Mar 2, 2015
    Posts:
    13

    Anyway thank you for the answer, but your answer for me was useless. I found why it was not working by myself. It cause in Unity system rotation over 180' is equaling -179'. So it cannot be assumed as solution if I saying that it rotate +359' for example. So I just translate everything to radians (0-2pi regarding to degree) all works fine now. ^^