Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Snapping an objects angle

Discussion in 'Scripting' started by Hybromech, Apr 30, 2018.

  1. Hybromech

    Hybromech

    Joined:
    Nov 25, 2017
    Posts:
    11
    Hi i have a simple issue that i cant work out here is what i am trying to do.

    I want to tell unity to snap an object to nearest right angle, so 89 degrees should snap to 90, 179 degrees should snap to 180, so the object can only ever be orientated in intervals of 90 degrees.

    Help appreciated.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,384
    Code (csharp):
    1.  
    2. float a = *arbitrary angle*;
    3. a = Mathf.Round(a / 90f) * 90f;
    4.  
     
  3. Hybromech

    Hybromech

    Joined:
    Nov 25, 2017
    Posts:
    11
    Ok i will give it a try
     
    Last edited: Apr 30, 2018