- Home /
 
Check if Rotation Angle is reached?
Hello, iam working on a puzzlegame where you have to rotate different maze peaces so that the player and the exit point are connected. so far so good.
for this game to work, i need to fire an event/call a funtion everytime the peace gets rotated by 90 degrees (every time you click on it). so, like this: 
the rotation code looks like this:
 using UnityEngine;
  using UnityEngine.EventSystems;
  using System.Collections;
  using qtools.qmaze;
  
  namespace qtools.qmaze.example
  {
      public class QMazePieceRotator : MonoBehaviour
      {
          private float targetAngle;
          private Quaternion targetRotation;
  
          void Awake () 
          {
              targetAngle = transform.eulerAngles.y;
              targetRotation = Quaternion.AngleAxis(targetAngle, Vector3.up);         
          }
  
          private float mouseDownTime;
          private Vector3 mousePosition;
  
          void OnMouseDown()
          {
              mouseDownTime = Time.realtimeSinceStartup;
              mousePosition = Input.mousePosition;
  
              if (Time.realtimeSinceStartup - mouseDownTime < 0.300 && Vector3.SqrMagnitude(Input.mousePosition - mousePosition) < 10)
              {
                  targetAngle = (targetAngle + 90) % 360;
                  targetRotation = Quaternion.AngleAxis(targetAngle, Vector3.up);         
              }
          }
  
          void OnMouseOver()
          {
              Vector3 position = QMazeSelector.getInstance().transform.position;
              position.x = transform.position.x;
              position.z = transform.position.z;
              if (QMazeSelector.getInstance() != null)
                  QMazeSelector.getInstance().transform.position = position;
          }
  
          void OnMouseUp()
          {
  
          }
  
          private void Update()
          {
              transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 10 * Time.deltaTime);
          }
      }
  }
 
               how & where should i call the check for the rotated piece? :) i would assume in the mouse up area but i somehow don't know how to check if the pice is rotated/the rotation finished
Answer by Duugu · Sep 02, 2015 at 03:03 PM
I would add a flag isRotating and set it to false as default. Then I would set the flag to true in OnMouseDown and check if transform.rotation is equal to targetRotation in Update if the flag is true. If transform.rotation is equal to targetRotation I would set the flag to false and call whatever you need to do if the rotation is finished.
Your answer