A problem with raycasting

For some reason, something went wrong. In theory, the white cube was supposed to spawn yellow cubes only above the yellow platform, but for some reason they start to appear above the baseplate, how can this be solved?

Screenshot for more information:

This only happens on the X axis.

Code:

local Limit = 1000
local Current = 0
local Spawner = workspace.GrassSpawner
local Radius = 100
while wait() do
   if Current < Limit then
      Current += 1
      local RandomPosition = Vector3.new(math.random(Spawner.Position.X - Radius, Spawner.Position.X + Radius), Spawner.Position.Y, math.random(Spawner.Position.Z - Radius, Spawner.Position.Z + Radius))
      local ray = Ray.new(Vector3.new(RandomPosition.X, Spawner.Position.Y + 1, RandomPosition.Z), Vector3.new(RandomPosition.X, Spawner.Position.Y - 100, RandomPosition.Z))
      local TallGrass = game:GetService("ReplicatedStorage").TallGrassFolder.TallGrass:Clone()
      local Hit = workspace:FindPartOnRay(ray)
      if Hit and Hit.Parent == workspace.Map.GrassFolder then
         TallGrass.Parent = workspace.Map.TallGrassFolder
         TallGrass.Position = Vector3.new(RandomPosition.X, Hit.Position.Y + (Hit.Size.Y / 2 + TallGrass.Size.Y / 2), RandomPosition.Z)
         TallGrass.Color = Hit.Color
         print("Hit")
      else
         Current -= 1
      end
   end
   for i, v in pairs(workspace.Map.TallGrassFolder:GetChildren()) do
      if (Spawner.Position - v.Position).Magnitude > Radius then
         v:Destroy()
         Current -= 1
      end
   end
end
4 Likes

Is Radius exactly equal to half the size of Spawner on both axes?

1 Like

And how can it affect? I just don’t quite understand

1 Like

If Radius is set larger than the actual radius of the part, the RandomPosition will generate a position outside of the part.

The problem here is not in a random position, but in the fact that there is an offset here. By default, yellow cubes should appear relative to the white cube

Raycast considers that there is a continuation of the part

Oh I see. I misinterpreted that Spawner was the field, not the white part. I thought that you were generating for the whole field based on the grass. My bad.

If raycast sees that there is a part from below, then it checks its parent, if it matches, then it puts the cube on it, if not, then it just tries to plant it again until it succeeds. (At least it should work that way)

1 Like

Your script works fine for me. Do you have anything else in workspace.Map.GrassFolder aside from the floor?

Nothing, maybe it was my local error? I don’t know :confused:

1 Like

Is your floor a meshpart/union or normal part?

Is a normal part (I’m already starting to dislike the minimum number of characters)

It wasn’t like that. Everything continues to deviate for me, just like before I restarted the Roblox studio

Do you think if I recreate the place, something will change?

Welp, since you don’t have any other parts in Workspace.Map.GrassFolder, the floor is a normal part, and the rays are all straight down, I can’t think of a reason of how this is happening. Maybe retest the script?

Here’s another test that worked just fine.

Try to increase the number of parts, as well as reduce the radius and put the spawn near the edge

1 Like

I do not know why this is happening… Perhaps this is a mistake of the engine itself

The spawn is already near the edge, I’ve both increased/decreased the radius as well as the quantity generated. I even moved your current count to only increase if a grass is generated.
I dunno. :man_shrugging: Like I mentioned earlier, make sure that there’s nothing else in Workspace.Map.GrassFolder.

Here’s the file if you want to compare it to your own.
Grass Gen Test.rbxl (31.4 KB)

I see you have the same mistake, you just have less spawning parts than mine, so it’s less noticeable.

This