Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by GigiB · Sep 15, 2014 at 07:11 AM · rotationsingamedaycycledaynight

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);
 
     }
 
         
         
             
     }
 
 }
 
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sethuraj · Sep 15, 2014 at 07:15 AM 0
Share

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 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · Sep 15, 2014 at 04:14 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Is there a way to make a day/night cycle (can be just light on/off) with a very high amount of lights? 1 Answer

Realtime daylight cycle? 1 Answer

(Day and Nigh Cycle) Terrain is lit up at night 1 Answer

Why is fading not smooth? 0 Answers

[Day/Night Cycle] Objects get too dark at night [SOLVED] 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges