Skip to content

Instantly share code, notes, and snippets.

@johnsoncodehk
Last active April 12, 2024 16:31
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnsoncodehk/2ecb0136304d4badbb92bd0c1dbd8bae to your computer and use it in GitHub Desktop.
Save johnsoncodehk/2ecb0136304d4badbb92bd0c1dbd8bae to your computer and use it in GitHub Desktop.
Unity Clamp Angle
public static float ClampAngle(float angle, float min, float max) {
float start = (min + max) * 0.5f - 180;
float floor = Mathf.FloorToInt((angle - start) / 360) * 360;
return Mathf.Clamp(angle, min + floor, max + floor);
}
@cstjJoeBoulet
Copy link

Your my saviour ! Thank you so much !

@Grave18
Copy link

Grave18 commented Apr 6, 2021

Thank you, simple clamp work not so good)

@Jeremi360
Copy link

Thank you

@sohrabhamza
Copy link

Thank you very much. Spend 3 hours trying to come up with this myself and failed.

@YaroslavFur
Copy link

YaroslavFur commented Apr 24, 2023

<3 (heart)

@Trapperfly
Copy link

You are amazing <3 Thank you

@Vladimir-Maks
Copy link

you helped me solve my problem with clamping angles in godot. Thank you so much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment