Right, the issue is probably that he thinks when his initial point is below the surface we should cast upwards. However that would not work because a raycast can only detect the surface of a collider from the outside. A raycast from the inside will not hit the collider.
Well in my case I would need to find a ceiling too, so two raycasts just to get a ground point. I was wondering if there was something built in navmesh or unity to do this with less code.
It still doesn’t make sense to do an upward raycast. Do what spiney199 suggested above. Make sure the raycast can pass through everything except spawnable surfaces.
using UnityEngine;
public class Spawner : MonoBehaviour // Spawn monsters somewhere off in the distance
{
public GameObject monster;
void Start()
{
Vector3 pos=Quaternion.Euler(0,Random.Range(0,360),0)*Vector3.right*30; // Get a random distant point
if (!Physics.SphereCast(transform.position,0.5f,pos,out RaycastHit hit,30)) // Nothing between us and the point?
if (Physics.Raycast(transform.position+pos,Vector3.down,out hit,2)) // Ground below the point?
Instantiate(monster,hit.point+Vector3.up,Quaternion.identity); // RAWR!!
Invoke("Start",0.5f); // keep doing it
}
}
Well It does make sense when you understand the reason behind the two raycasts. Imagine a 3d level with several different hallways and levels. Choosing a new random location on the height of your player, that potential new location could be in a hallway at a higher elevation. So casting downwards wouldn’t get you a proper position. So he first searches for the ceiling and then does another raycast from the ceiling down to find the floor. Though what doesn’t really make sense to me is this default value:
float castPosY = rayLength;
So when the potential position is below your current height, he would start the downward ray at an arbitrary absolute position that is equal to the ray length, so relative from absolute 0 rayLength upwards. Technically, depending on the level layout, that point at a height of 100 could still be below a potential valid location as this is not relative to the current player position but an absolute height in worldspace.
I would generally start with a single raycast downwards from the player center height to find the floor. If there is no floor below, do a raycast upwards to find the closest ceiling at this location and then do another raycast down to find the floor below that ceiling point. Just going up a fix amount and casting down would cause many issues if your level has multi layers / levels stacked on top of each other as you would always just hit the top floor.
Though just picking a random point in a rectangle around the player also isn’t a great way to navigate anyways However this all depends on the level layout.