Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 MounirDev · Sep 16, 2019 at 11:40 AM · rotationunity 2dspaceplanetrotation detection

how to know when rotation is completed ? 2d

hi i'm making a simple 2d soloar system game, and i want to increment days each time earth does one rotation around itself ( note that it's also orbiting the star) . now i tried checking the z rotation, if it's in a certain range then i do my thing, but since it's in the update function, if earth makes one rotation, days don't increment by one each rotation. so i'm wondering if there is a certain method that i can use to check the rotation ? here is a picture :alt text

2019-09-16-1.png (48.9 kB)
Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by BenderBlai · Sep 16, 2019 at 12:30 PM

So days don't increment when the earth is rotating and therefore the script doesen't work? If thats the case, it is probably because you are checking the number with the "==" operator, and you are rotating every frame with a decimal angle (or it is being mutiplied by "Time.deltaTime"). That makes the rotation not likely to stay in integers, and, if you are checking the value with an integer value, not to return true. There are different solutions to this, hope it helps.

Comment
Add comment · Show 1 · 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
avatar image MounirDev · Sep 16, 2019 at 02:51 PM 0
Share

Thank you, so what's the best way to check a rotation value? i searched about this and found that should make a rang of values, but that causes a problem since too many frames are running between these values which will increment my variable several times. idk i'll try to work with properties, maybe that's way.

avatar image
0

Answer by suIly · Sep 18, 2019 at 01:05 AM

You should use (Mathf.RoundToInt(rotation.z)) == 360 to check.

Comment
Add comment · Show 1 · 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
avatar image MounirDev · Sep 18, 2019 at 01:33 AM 0
Share

@jacksonacademyashmore thank you, I tried that but it always give 0. it worked when i replaced " transform.rotation.z" by a float value, but when i set a variable ins$$anonymous$$d it gives 0. any idea why?

avatar image
0

Answer by sisse008 · Sep 16, 2019 at 12:03 PM

Rotation around the sun or around it self? if around the sun than you should probably check position to find if it made full circle.

Comment
Add comment · Show 4 · 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
avatar image MounirDev · Sep 16, 2019 at 12:14 PM 0
Share

around itself, i'm checking days

avatar image sisse008 MounirDev · Sep 16, 2019 at 01:04 PM 0
Share

I think i'm starting to understand your question. so, you've found a way to check if earth did full circle around it self. you are wondering if there is a known "good practice" way of doing it because you are having trouble incrementing the variable "days" in the update function. So there is no obvious way of doing this. if you look online you'll see many solutions, none of which seem easy or obvious.

about the incrementing days thing. try using a Boolean. use the same function you have to see if earth rotated 360 to see if earth rotated, say, 180 degrees.

 bool increment = true;
     int days = 0;
     
     void Update()
     {
           if(FullRound() && increment)
            {
                    days++;
                    increment = false;
            }
            else if(HalfRound())
                     increment = true;
     
     }
     
     
     bool FullRound(){}
     
     bool HalfRound(){}
 

 

avatar image MounirDev · Sep 16, 2019 at 02:45 PM 0
Share

@sisse008 actually the whole problem is in FullAround() function, how to check? if it's by range like : if( transform.rotation.z <360 && transform.rotation.z >359 ) . days variable will keep incrementing in the updating functions in the period between 359 and 360. i'll try to use Properties, i think that's the best way.

avatar image sisse008 MounirDev · Sep 16, 2019 at 03:06 PM 0
Share

in this case, the boolean solution holds:

 bool newRound = true;
      int days = 0;
      
      void Update()
      {
            if(FullRound() && newRound)
             {
                     days++;
                      newRound = false;
                    
             }
             else if(HalfRound())
                      newRound = true
                     
      
      }
      
      
      bool FullRound()
     {
            if(transform.rotation.z < 360 && transform.rotation.z > 360 - x && 
             newRound)
                    
                      return true;
              return false;
      }
      
      bool HalfRound()
      {
                if(transform.rotation.z < 180 && transform.rotation.z > 180 - x && 
               !newRound)
                         
                          return true;
                return false;
      }
avatar image
0

Answer by Skeasy · Sep 16, 2019 at 01:07 PM

[Disclaimer: Beginner and not a native speaker]

I have tried it myself and came up a solution, but i dont know if you will like it :)

I have a sprite that rotates with the following script: "transform.eulerAngles += new Vector3(0, 0, 5);"

then i made two empty objects as childs of the earth and gave them the names "rota" and "stay".

I gave both a little box container (as trigger) and a rigidbody with continous collision detection (because they are small and fast).

I tagged "rota" as "rota" and gave "stay" the following script:

(Ignore the UI elements, i used them to count the rotations)


public class checkDayPassage : MonoBehaviour { public int rotations = 0; public Text txt;

 void Update()
 {
     transform.eulerAngles += new Vector3(0, 0, -5); 
     txt.text = "Rotations: " + rotations;
 }

 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.tag == "rota")
     {
         rotations++;
     }
 }

}


What happens? The earth rotates. "Stay" rotates in the opposite direction to compensate the movement and stay at its place, and checks if "rota" passes by. "Rota", as child of earth just rotates like earth. If the childs touch each other they increment the value of the rotations.

Hope this helps in some way :)

Comment
Add comment · Show 5 · 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
avatar image MounirDev · Sep 16, 2019 at 03:14 PM 0
Share

why that script is supposed to stop Rota ? it will be spinning, and rotating with earth as well! cuz it's a child : alt text

2019-09-16-4.png (27.4 kB)
avatar image Skeasy MounirDev · Sep 17, 2019 at 03:07 PM 0
Share

Hi @$$anonymous$$ounirDev ,

there was a misunderstanding. Look at my picture. The child rotates with the same circle as the parent, not his own. I tested this. (I changed the project already, but if you really want, i could make it again and send it to you. But I think there are better ways then $$anonymous$$e.)

I dont know why, but this site doesnt let me upload a picture, so I uploaded it here: https://ibb.co/1qhByLG

$$anonymous$$fG Skeasy

avatar image MounirDev · Sep 17, 2019 at 03:23 PM 0
Share

@Skeasy "stay" will rotate around itself and move with the earth at the same time because it's a child, the script you have on "stay" don't stop it from changing it's position, it rotates its in the same place in the opposite direction of the earth's rotation ( you probably didn't make "stay" a child, that's why it worked with you) . i did something similaire and it worked fine ( i gave the sun a collider and made it face the earth always so when the moon triggers the sun it counts like one day) here is a picture :alt text thank you so much for you help :)

2019-09-17.png (87.0 kB)
avatar image Skeasy MounirDev · Sep 18, 2019 at 09:10 AM 0
Share

Hi @$$anonymous$$ounirDev

good idea!

I have tried it again and found the reason why your childs are spinning around themselfs :) The childs have to be at the same position as the parent. I moved just the collider, not the object itself. To tolerate movement of earth i added a script to the childs rota and stay which does this:

void Update() { gameObject.GetComponent().position = earth.position; }

This way it works pretty good. (But with your idea, I dont even need "rota". Just a second collider for earth.) Glad I could help you. :)

avatar image MounirDev · Sep 18, 2019 at 11:57 AM 0
Share

@Skeasy oh now i see what you did :) thank you so much for your help !

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

151 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Making Top-Down spaceship movement, getting current speed, acceleration, without rigidbody! 1 Answer

GUI Display 2 Answers

What is going on with Zxing Libary in portrait mode? 0 Answers

Rotate one gameobject in 90 degree increments based on another gameobjects rotation. 0 Answers

Replicating the rotation of one game object on another game object using Quaternions. 0 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