- Home /
Do something every few degrees of a rotation?
Hi,
I have an object which can be rotated. I want to execute something every 15 degrees of rotation.
Right now I set it up like this:
 void Update()
 {
     float currentAngle = this.transform.eulerAngles.z;
     if (currentAngle > 14.5f && currentAngle < 15.5f)
     {
         "do something"
     }
     .
     .
     .
 }
and I have repeated this for every 15 degrees which works pretty well but seems way too complicated and not efficient at all to me.
Is there a more efficient way to approach this and if so how would you execute that?
Thanks in advance :)
Answer by Hellium · Dec 16, 2019 at 10:24 AM
Disclaimer : Code not tested
 private float step = 15;
 private float deltaAngle = 1;
 private bool stepReachedLastFrame;
 
 void Update()
 {
     float currentAngle = transform.eulerAngles.z;
     bool stepReached = (currentAngle + deltaAngle * 0.5f) % step < deltaAngle;
     if (stepReached && !stepReachedLastFrame)
     {
         Debug.Log("Entering zone");
     }
     else if (!stepReached && stepReachedLastFrame)
     {
         Debug.Log("Leaving zone");
     }
     stepReachedLastFrame = stepReached;
 }
Thanks a lot! works perfectly! I think you mixed up "delta" and "deltaAngle" but when I changed it it was flawless!
Thanks again! :)
Your answer
 
 
             Follow this Question
Related Questions
how to rotate a Rigidbody2D player on a moving platform. 0 Answers
How Do I rotate a Object Based On the player's location? 1 Answer
Cannot reduce localScale to 0 1 Answer
Rotate an object to a given point 2 Answers
Why when using get; set; in one script i'm getting null in other script that use it ? 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                