I have a raycast that is meant to find a random position on the surface of an object (a procedurally generated plane that represents the ground). Since for now the object is a square on the x and z axes, but irregular on the y axis, I’m trying to do this by taking a random point on the x,z plane and then raycasting down from above the plane and using the hit point as a point on the surface of the ground object.
However, the raycast on line 61 is always returning false and I can’t figure out why…any ideas?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class PickupSpawner : MonoBehaviour
{
[SerializeField]
public List <GameObject> pickupObjects;
public Vector2 minMaxSecondsBetweenSpawns;
public float maxObjectsInScene = 5f;
public GameObject groundPlane;
private List<GameObject> activePickupsInScene;
private bool currentlySpawning = false;
LayerMask layerMask;
private void Start()
{
layerMask = LayerMask.GetMask("Ground");
}
// Update is called once per frame
void Update()
{
activePickupsInScene = GameObject.FindGameObjectsWithTag("Type0").ToList();
if (activePickupsInScene.Count < maxObjectsInScene && !currentlySpawning)
{
StartCoroutine (spawnNewPickup());
}
}
IEnumerator spawnNewPickup()
{
currentlySpawning = true;
int randomItemIndex = Random.Range(0, pickupObjects.Count);
GameObject newPickup = Instantiate(pickupObjects[randomItemIndex], pickRandomLocation(), Quaternion.identity) as GameObject;
newPickup.tag = PickupObjects.setPickupType(pickupObjects[randomItemIndex].name);
float waitCount = Random.Range(minMaxSecondsBetweenSpawns.x, minMaxSecondsBetweenSpawns.y);
yield return new WaitForSeconds(waitCount);
currentlySpawning = false;
}
private Vector3 pickRandomLocation()
{
float terrainScaleX = groundPlane.transform.localScale.x * 0.5f;
float terrainScaleY = groundPlane.transform.localScale.y * 0.5f;
Vector3 randomLocation = new Vector3(Random.Range(-terrainScaleX, terrainScaleX), 25f, Random.Range(-terrainScaleY, terrainScaleY));
Ray ray = new Ray(randomLocation, Vector3.down);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, layerMask)) {
Debug.Log("Ray hit ground plane at " + hit.point);
return hit.point;
}
else
{
Debug.Log("Raycast failed!");
return Vector3.zero;
}
}
}