Why are my Physics.RayCast not showing the collsion with the Ground?
Hello! I have an issue where my RayCast always returns (0,0,0) as point of collision. I am trying to set random positions of enemies (x and z) and place them under the map at a set y value and then cast a ray upwards to detect where it collides with the Ground (I am using Blenders generated plane which isn't convex but i have made sure to enable Mesh Colliders and have checked that they do display correctly). When i cast a ray i always get a result of (0,0,0) as collision point with the ground or in other words no collision. Sometimes howerve i got collision points but only for 1 out of 50 enemies. I have also drawn the same rays i am casting using Debug.DrawRay and they do intersect with the Blender's Plane. They only thing i can think of is that Ground colliders is simply too thin? I have also checked multiple layerMakss values (such as all and none) and have set Ground's layerMask to Ground.
CODE (for loop where i generate enemies and try to get collision point between new enemy and the ground I'm not sure if this is the best way to spawn enemies but i think it should work so i would be very thankful if anyone has any idea what have i done wrong. THANKS A LOT!
for (int i = 0; i < numOfBasicEnemies; i++)
{
GameObject newEnemy = Instantiate(basicEnemy, enemiesParentObject);
// Set new position of an enemy
Vector3 tempPos = new Vector3(0,-2 * mapHeight, 0); // Set position under the map 2* mapHeight under the map (under y = 0)
tempPos.x = Random.Range(-xSpawnLimit, xSpawnLimit);
tempPos.z = Random.Range(-zSpawnLimit, zSpawnLimit);
Vector3 rayOrigin = tempPos + new Vector3(0, 20, 0); // Start the ray a bit above the enemy
Vector3 rayDirection = Vector3.up; // Point the ray upwards
RaycastHit rayCastHitInfo; // Ray call back object
Physics.Raycast(rayOrigin, rayDirection, out rayCastHitInfo, 4 * mapHeight, layerMask);
// ^^ -> cast ray 2 * mapHeight above the map (above y = 0)
Debug.DrawRay(rayOrigin, rayDirection * (4 * mapHeight), new Color(255, 0, 0), 100f, true);
Vector3 groundPoint = rayCastHitInfo.point;
Debug.Log(groundPoint);
newEnemy.transform.position = tempPos;
newEnemy.GetComponent<basicEnemy>().color = gameVaraibles.colorOptions[Random.Range(0, gameVaraibles.colorOptions.Length)];
//newEnemy.GetComponent<Rigidbody>().useGravity = true;
newEnemy.GetComponent<basicEnemy>().loadEnemy();
newEnemy.GetComponent<CapsuleCollider>().enabled = true;
}
basicEnemy.SetActive(false);
This is a fully working script (this is an example).
You need to look at lines 22 - 23, there lies the method you need.
Explanatory video - https://www.youtube.com/watch?v=b9Hhgz$$anonymous$$uYx$$anonymous$$
using UnityEngine;
public class Spawn : $$anonymous$$onoBehaviour
{
public GameObject enemy;
public float radiusSpawn = 10;
public int numOfBasicEnemies = 10;
public Layer$$anonymous$$ask groundLayer;
Transform tr;
RaycastHit hit;
void Awake()
{
tr = transform;
}
void Start()
{
for (int i = 0; i < numOfBasicEnemies; i++)
{
Vector2 randomCircle = Random.insideUnitCircle * radiusSpawn;
Vector3 v3rc = new Vector3(tr.position.x + randomCircle.x, 100, tr.position.z + randomCircle.y);
if (Physics.Raycast(v3rc, Vector3.down, out hit, 150, groundLayer))
{
GameObject enemyRay = Instantiate(enemy);
enemyRay.transform.SetPositionAndRotation(new Vector3(v3rc.x, hit.point.y + 2.0f, v3rc.z), tr.rotation);
enemyRay.transform.parent = tr;
}
}
}
}