Check if a random position is inside a collider (Vector 2D)
Hey everybody, my first post and also my first game being developed in unity:
I've been using a Random.Range so that I can define where the GameObject can randomly go within that area, but sometimes it gets inside the colliders (obviously) and I wanted a way to avoid that:
int x;
int y;
Vector2 pos;
void Start () {
x = Random.Range(0, 40);
y = Random.Range(0, 42);
pos = new Vector2(x, y);
transform.position = pos;
}
I've been trying to use a SweepTest, but unfortunately it's only compatible with vector3 and I wanted to use as a vector2...
I also could manually set some positions and make the GameObject pick one of it randomly, but I didn't see anything like that while I was searching for it.
So any help would be appreciated :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612101710im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Trigger Collinder doesnt work with Input.GetKey 1 Answer
Constant position in multiplayer 0 Answers
[2D] Make player push object 1 Answer
return to start position once hit with other collider 1 Answer