How to check the rotation of a transform

Hello I wanted to know if their is a way to check to see at what point in rotation is an object. For example using an if statement to see if thee sun is at 270 degrees and it’s rotation and then Debugging "It's noon". The reason why I need this is because I’m trying to track the days in game but first I need to have a clock.

I figured the best way to script a change in the day is to trigger it when the sun is at the Midnight(90 degrees) position.

I hope my question is clear, and thank you ahead of time

Here’s the script btw:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public class GameTime : MonoBehaviour {
	public enum TimeOfDay
		{
		Idle,
		SunRise,
		SunSet,
		}
	public Transform[] sun;
	public float dayCycleInMinutes = 1;    //Tells of how many IRL minutes is a whole day in game

	public float sunRise;                   //the time of day that we start the sunrise
	public float sunSet;                    // The time of day that we start the sun set
	public float skyboxBlendModifier;       //The speed at which the textures in the skybox blend

	private Sun[] _sunScript;
	private float _degreeRotation;
	private float _timeOfDay;


	private float _dayCycleInSeconds;

	private const float SECOND = 1;
	private const float MINUTE = 60 * SECOND;
	private const float HOUR = 60 * MINUTE;
	private const float DAY = 24* HOUR;
	private const float DEGREES_PER_SECOND = 360 / DAY;     // Sun goes 360 means One full day

	private TimeOfDay _tod;







	#region
	// Use this for initialization
	void Start () 
	{

		_tod = TimeOfDay.Idle;
		_dayCycleInSeconds = dayCycleInMinutes * MINUTE;

		RenderSettings.skybox.SetFloat ("_Blend", 0);

		_sunScript = new Sun[sun.Length];
		for (int cnt = 0; cnt < sun.Length; cnt++) 
		{
		 Sun temp = sun[cnt].GetComponent<Sun>();

			if(temp == null)
			{
				Debug.LogWarning("Sun script not found. Adding it");
				sun[cnt].gameObject.AddComponent<Sun>();
				temp = sun[cnt].GetComponent<Sun>();
			}
			_sunScript[cnt] = temp;
			sunRise *= _dayCycleInSeconds;
			sunSet *= _dayCycleInSeconds;
		}


		_timeOfDay = 0;
		_degreeRotation = DEGREES_PER_SECOND * DAY / (_dayCycleInSeconds);


	}
	#endregion
	
	// Update is called once per frame
	void Update () {


		for(int cnt = 0; cnt< sun.Length; cnt++)
		sun[cnt].Rotate(new Vector3(_degreeRotation, 0, 0) * Time.deltaTime);
	
		_timeOfDay += Time.deltaTime;

		if (_timeOfDay > _dayCycleInSeconds) 
				_timeOfDay -= _dayCycleInSeconds;

		//Debug.Log (_timeOfDay);

		if (_timeOfDay > sunRise && _timeOfDay < sunSet && RenderSettings.skybox.GetFloat ("_Blend") < 1)
		{
						_tod = GameTime.TimeOfDay.SunRise;
						BlendedSkybox ();
		} 
		else if (_timeOfDay > sunSet && RenderSettings.skybox.GetFloat ("_Blend") > 0) 
		{
						_tod = GameTime.TimeOfDay.SunSet;
						BlendedSkybox ();

		} 
		else 
		{
			_tod = GameTime.TimeOfDay.Idle;
		}


	}

	private void BlendedSkybox()       //Handels transition og day and night skyboxes
	{
		float temp = 0; 

		switch (_tod) 
		{
		case TimeOfDay.SunRise:
			temp = (_timeOfDay - sunRise) / _dayCycleInSeconds * skyboxBlendModifier;
			break;
		case TimeOfDay.SunSet:
			temp = (_timeOfDay - sunSet) / _dayCycleInSeconds * skyboxBlendModifier;
			temp = 1 - temp;
			break;
		}


		RenderSettings.skybox.SetFloat("_Blend", temp);

		//Debug.Log (temp);

	}

		
		
			
	}

}

You can use transform.rotation.eulerAngles to get the roation of any object on any axis within 0 to 359 degrees.You can use this to read the rotation on Global coordinate system.but dont pass a value to this unless its absolute rotation value( ie between 0 - 359) //Get the rotation in Y axis int RotationInDegrees = (int)transform.rotation.eulerAngles.y; //Log the rotation in integer Debug.Log(RotationInDegrees); Hope this helps

1 Answer

1

Reading 0-360 rotations from an object is almost always an error-prone mess. The only reliable way is to have your own variable, used for movement, and to just read that.

For example, sunAng+=1; sunStick.rotation = Quaternion.Euler(-10,20,sunAng); (-10 in x is to move the sun a bit towards the equator.) Rotation here is around z, for an East to West spin. I’m assuming the sun is at the end of a long stick (couldn’t figure out how you were moving yours.) Then just check your var sunAng to see where it is.