I have a terrain. It’s size 500x500 with center in (0,0).
I want to create ray, but sometimes I have null reference exception, because my ray not colliding to any objects.
My code:
pos = new Vector3(Random.Range(-200f, 200f), 400f, Random.Range(-200f, 200f));
Ray ray = new Ray(pos, Vector3.down);
Physics.Raycast(ray, out hit, 50000);
if (hit.collider.name == "Terrain") //sometimes null reference exception
{
//do actions
}
I see what Ray.origin is not same as “pos” vector. It is normal?