Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Round an integer from 17 to 20(example)

Discussion in 'Scripting' started by David110691, May 4, 2021.

  1. David110691

    David110691

    Joined:
    May 21, 2019
    Posts:
    10
    Hello,
    I want to get my hit.point from the Raycast in ten steps
    for example: from 17.25f it should round to 20 or from 14.16f to 10
    I thought i can do that with Mathf.round but didnt find a way to do that with it yet.
    i know i can round the float 17.25f to 17 but then i dont know how to continue.
    Heres My Code:
    Code (CSharp):
    1. public void GridInteract()
    2.     {
    3.         Camera cam = GameObject.Find("Main Camera").GetComponent<Camera>();
    4.         Ray RayPlacing = cam.ScreenPointToRay(Input.mousePosition);
    5.         RaycastHit PlaceObjectHit;
    6.         Vector3 mousePosition = Input.mousePosition;
    7.  
    8.         if (Input.GetMouseButtonDown(0))
    9.         {
    10.             if (Physics.Raycast(RayPlacing, out PlaceObjectHit, Mathf.Infinity))
    11.             {
    12.                 PlaceObjectHit.point = new Vector3(PlaceObjectHit.point.x, 0, PlaceObjectHit.point.z);
    13.  
    14.                     if (currentBlueprint != null)
    15.                     {
    16.                         Vector3 GridPosition = new Vector3((int)PlaceObjectHit.point.x,
    17.                             (int)PlaceObjectHit.point.y,                                                                                                                                    (int)PlaceObjectHit.point.z);
    18.  
    19.                             currentBlueprint.tag = "ShopObject";
    20.                             currentBlueprint.transform.position = GridPosition;
    21.                             currentBlueprint.SetActive(true);
    22.                      
    23.  
    24.                         if(!Map_Script.script.PositionX.Contains((int)GridPosition.x))
    25.                             Map_Script.script.PositionX.Add((int)GridPosition.x);
    26.                         if(!Map_Script.script.PositionX.Contains((int)GridPosition.z))
    27.                             Map_Script.script.PositionX.Add((int)GridPosition.z);
    28.  
    29.                         currentBlueprint = null;
    30.                 }
    31.             }
    32.         }
    33.     }
    I hope u understand what im seaching for cuz im not that good at english....
    thanks for u help :)

    David
     
  2. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    Code (CSharp):
    1. Mathf.RoundToInt(myFloat / 10f) * 10;
     
    Bunny83, ensiferum888 and David110691 like this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,894
    Something like this should work:
    Code (CSharp):
    1. public float RoundToNearest(float input, float nearest) {
    2.   float scaled = myFloat / roundToNearest;
    3.   float rounded = Mathf.Round(scaled);
    4.   return rounded * roundToNearest;
    5. }
    6.  
    7. void Start() {
    8.   float rounded = RoundToNearest(17.25f, 10f);
    9.   Debug.Log("The rounded value is: " + rounded);
    10. }
     
    ensiferum888 likes this.
  4. David110691

    David110691

    Joined:
    May 21, 2019
    Posts:
    10
    that worked... Thank you