Home > OS >  Move to area in circle, then snap to a grid?
Move to area in circle, then snap to a grid?

Time:05-06

Beginner here. I am trying to get this to find a random point inside a circle, go to that point but then snap to a grid.

I have been able to get a point inside the circle and move there, but I haven't been able to figure out any working methods.

This is the code to find and move to a point inside the circle.

private void RandomizePosition()
{
    double x = -0.5; // center of circle x
    double y = -0.5; // center of circle y

    this.transform.position = UnityEngine.Random.insideUnitCircle * 7; // this makes the circle
}

I have tired using this.transform.position = new Vector3(Mathf.Round(x), Mathf.Round(y), 0.0f); but I get the error "cannot implicitly convert type double to float" If anyone has any ideas please share, thanks.

CodePudding user response:

Mathf.Round takes a float as its argument and you are passing a double in it. The following should work:

this.transform.position = new Vector3(Mathf.Round((float)x), Mathf.Round((float)y), 0.0f);

CodePudding user response:

use float directly:

var x = -0.5f; // center of circle x
var y = -0.5f; // center of circle y
  • Related