Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6155065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:22:16+00:00 2026-05-23T20:22:16+00:00

Heads up: Even though this problem arose while I was working with Unity, it

  • 0

Heads up: Even though this problem arose while I was working with Unity, it has nothing specific to Unity, and is more about programming logic, so please don’t shy away.

I’m using Unity and rotating an object by script. The thing is, if I rotate it to, say, 180 degrees, the object does not rotate exactly to that much and tends to stop at between 179 and 181 degrees. So, to check if rotation is complete I check if the rotation angle is targetAngle +/- 1, which works.

I check using

if (transform.eulerAngles.z > lowerLimit && transform.eulerAngles.z < upperLimit)

where

lowerLimit = targetAngle-1;
upperLimit = targetAngle + 1;

Now, the problem arises when the targetAngle is 0. In this case, my script checks if rotation angle is between -1 and 1. But, -1 should really be 359, so it needs to check if the angle lies between 359 and 1.

How can I implement this?
In other words, I guess I’m asking how to implement a wrap-around number system.

EDIT

Found one work-around. If targetAngle is 0, I treat is specially. It works, but isn’t the most elegant.

         if (targetAngle == 0.0)
        {
            if ((transform.eulerAngles.z > 359.0 && transform.eulerAngles.z <= 360.0) || (transform.eulerAngles.z >= 0.0 && transform.eulerAngles.z <= 1)) 
            {
                rotate = false;            
            }
        }
        else
        {
            if (transform.eulerAngles.z > targetAngle - 1 && transform.eulerAngles.z < targetAngle + 1)
            {
                rotate = false;            
            }
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:22:16+00:00Added an answer on May 23, 2026 at 8:22 pm

    You could do …

    lowerLimit = (targetAngle % 360) + 359; //360 - 1;
    upperLimit = (targetAngle % 360) + 361; //360 + 1;
    
    if (((transform.eulerAngles.z + 360) % 360) > lowerLimit
     && ((transform.eulerAngles.z + 360) % 360) < upperLimit)
    

    This moves the check away from the zero and you wouldn’t have to deal with positive/negative checking.

    EDIT

    The % operator on the targetAngle restricts the rotating to +/-359 degrees, so a target angle of 721 would come down to 1, and a target angle of -359 would come down to 1. This should do nicely for all cases I think.

    EDIT 2

    To fix the last case you mentioned in your comment, I guess you’d need to apply the same wrapping logic to your transform.eulerAngles.z values. Probably best to put this wrapping in an extra function now, so try this:

    int wrapNumber(int input) // replace int with whatever your type is
    {
      // this will always return an angle between 0 and 360:
      // the inner % 360 restricts everything to +/- 360
      // +360 moves negative values to the positive range, and positive ones to > 360
      // the final % 360 caps everything to 0...360
      return ((input % 360) + 360) % 360;
    }
    
    lowerLimit = wrapNumber(targetAngle) + 359; //360 - 1;
    upperLimit = wrapNumber(targetAngle) + 361; //360 + 1;
    
    if (wrapNumber(transform.eulerAngles.z) + 360 > lowerLimit
     && wrapNumber(transform.eulerAngles.z) + 360 < upperLimit)
    

    Depending on how often you need to use this, checking for some cases might remove unneeded overhead. For example, the final % 360 within wrapNumber is only needed if the input was positive. If you’re calling this ten times per minute it probably won’t matter. If you’re calling it a hundred times per second, you may want to check how it performs in this situation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem of web pages being cached even though I specify that
Git has a much-touted(?) octopus-merge capability that can merge many heads into one. But
Ive been smashing my head with this for a while. I have 2 completely
Heads up: This is a weird question. I've got some really useful macros that
I cannot figure out why my code continues to run even though it appears
Ok, I have been banging my head on this one for a while, figure
I've found a couple similar posts regarding this same problem, but none of the
I have this content box on my site, that has different tabs on the
Anyone know off the top of their heads how to convert a System.Xml.XmlNode to
Just wondering if anyone knew off the top of their heads if there was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.