I want to check if the position I generated has another object or not. I do this by using Raycast:
private void RandomOBS()
{
Vector3 pos = new Vector3(UnityEngine.Random.Range(-8f, 8f), UnityEngine.Random.Range(-4f, 4f), 0);
while (Physics2D.Raycast(pos, pos).collider)
pos = new Vector3(UnityEngine.Random.Range(-8f, 8f), UnityEngine.Random.Range(-4f, 4f), 0);
}
The code above doesn't work at all. I tested it with if statement and for some reason it cannot check that position.
private void RandomOBS()
{
Vector3 pos = new Vector3(UnityEngine.Random.Range(-8f, 8f), UnityEngine.Random.Range(-4f, 4f), 0);
while (Physics2D.Raycast(transform.position, transform.position).collider)
pos = new Vector3(UnityEngine.Random.Range(-8f, 8f), UnityEngine.Random.Range(-4f, 4f), 0);
}
However when you check the transform.position
instead of the pos
, it works? How and why? All the game objects have the correct colliders & stuff.