Raycasting / Why are my procedurally generated plants only spawning on half of the plane?
for (int j = 0; j < startingFlowers; j++)
{
var flowerPosition = new Vector3(Random.Range(-planeSize, planeSize),
0f,
Random.Range(-planeSize, planeSize));
if (Physics.Raycast(flowerPosition, -Vector3.up, 0.05f, whatIsGround))
{
Instantiate(flower, flowerPosition, Quaternion.identity, ground.transform);
flowersSpawned++;
}
else j--;
}
This is the code I use to instantiate the flowers, but somehow after I added the Physics.Raycast it now only spawns flowers exactly on half the map. So the X value will go negative and positive, but the Z value for some reason never goes negative. Somehow I have a feeling its the layermask whatIsGround, but really the only object that is on that layer, is a single plane 50 by 50. Somehow the raycast doesnt recognize the Ground layer at positions where Z is negative.
The plane is not rotated, all rotational axes are at 0. The parent is an empty gameobject that is on another layer, changing that does nothing.
Somehow the raycast only recognizes half of my Plane on the layer "Ground". How is that even possible?
Unless im missing something really obvious (I hope so!)
Please let me know if anyone has encountered this issue before.
Here is an image:
Answer by zevonbiebelbrott · Apr 22, 2021 at 06:57 PM
I fixed this issue by changing the y position from 0 to 0.01f. It seems to be a common issue that has been around for a long time if you really dig deep into the forums. But this is the answer, the raycast will passthrough the plane if you start it at 0, somehow it doesnt pass through the plane if z is positive, so maybe even when it says rotation (0,0,0) there must be some very miniscule rotation that causes this.
Your answer
Follow this Question
Related Questions
LayerMask Not working when raycasting to Specific layer. 0 Answers
Get Direction From Raycast 0 Answers
RayCast not accurate on non-colliders (3D) 1 Answer
Pseudo 3D like in Wolfenstein 3D 1 Answer
Hit a sphere from inside with raycasting 0 Answers