- Home /
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 :
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.
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.
Answer by suIly · Sep 18, 2019 at 01:05 AM
You should use (Mathf.RoundToInt(rotation.z)) == 360 to check.
@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?
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.
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(){}
@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.
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;
}
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 :)
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
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
@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 : thank you so much for you help :)
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. :)
@Skeasy oh now i see what you did :) thank you so much for your help !
Your answer
Follow this Question
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