Question by
landownerhalo · Feb 21 at 06:48 PM ·
rotationspin
How to tell when transform rotated 360 degrees
I want to give a score if the player rotates 360 degrees from its current rotation position. I want a full spin.
Here is the code:
public class SpinScore : MonoBehaviour
{
float currentZRotation;
void Update()
{
currentZRotation = UnityEditor.TransformUtils.GetInspectorRotation(gameObject.transform).z;
Debug.Log("z rotation: " + currentZRotation);
if(currentZRotation >= 360 || currentZRotation <= -360)
{
Debug.Log("You spinned");
}
}
}
Comment
UnityEditor.TransformUtils
, being part of UnityEditor
, is available in editor only (error on standalone builds).
Try this
public class SpinScore : MonoBehaviour
{
[SerializeField] Vector3 _rotationLastFrame;
[SerializeField] Vector3 _rotationAccumulated;
void Start ()
{
_rotationLastFrame = transform.eulerAngles;
_rotationAccumulated = Vector3.zero;
}
void Update ()
{
_rotationAccumulated += transform.eulerAngles - _rotationLastFrame;
_rotationLastFrame = transform.eulerAngles;
if( Mathf.Abs(_rotationAccumulated.z)>360 )
{
Debug.Log("spinned 360 degrees (Z axis)");
_rotationAccumulated.z %= 360f;
}
}
}