Skip to main content

2d Raycast detection doesntdoesn't work

I want to check if the position I generated has another object or not. iI 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 doesntdoesn't work at all. iI 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);
}

howeverHowever when you check the transform.positiontransform.position instead of the pospos, it works?!? how How and why. all? All the game objects have the correct colliders & stuff.

2d Raycast detection doesnt work

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 doesnt 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.

2d Raycast detection doesn't work

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.

Source Link
ZozeR
  • 42
  • 8

2d Raycast detection doesnt work

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 doesnt 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.