How to have a localscript do this automatically?

Hello, everyone!

I’m wanting to create localized grass crumbs so that players can toggle them on or off, and they would only show up on terrain cells or parts with the material Grass.

Here’s a screenshot of what I’ve done by hand:

Any help would be greatly appreciated!

You want to completely automate the grass system so it is all generated, correct?

Toggling
Well to simply turn on/turn off rendering you’d want to make it so your system can easily be toggled on and off.

This can be a challenging thing to do, as you can’t just parent the grass into nil, as they will still be in memory. You need to have an automated system for actually creating the grass - in this case you’d either want to automate the grass completely, or create a big long ModuleScript that stores the location and data of every grass particle so that they may be generated by script.

The latter is pretty time-consuming and you’d probably want a plugin to do that. That leaves the first option which makes the toggling much easier, as it means you can just run the generation code again when the player enables the grass, and deletes the existing grass when it’s disabled.

Generation
I think the solution to this is raycasting, as long as you aren’t casting lots of rays every frame.

Are you using parts to hold the particles? If so, the amount of raycasting you would need to do would get decreased by heaps if you had, say, each part be 5x5 studs in X and Z.

You’d want to raycast in a radius of the player with an increment of 5 studs every time so that the parts match up, and if you had your radius be like 40 studs that, that’s ~78 parts total being rendered, which isn’t too bad I believe.

(You could also make the grass particles have a higher transparency based on their distance from the character so they don’t just appear to *stop* suddenly.)

Materials
Raycasting could also be used to determine the terrain material the grass will be on, so you can whitelist the Grass and similar terrain materials.

2 Likes

The grass is done using parts currently - and I’m planning on destroying the grass upon a player clicking a button to toggle it off.

I honestly think this might be a bit out of my league, unless I somehow make parts generate on terrain cells that have their material set to grass. Any way to do that method perhaps?

That’s what I meant by raycasting. e.g.

local RADIUS = 40
local PART_SIZE = 5
local NUM_PARTS = 78

local function Generate(CharacterPosition)
     -- delete existing grass
    local RadiusInParts = RADIUS / PART_SIZE
    local NumberOfAngles = NUM_PARTS / RadiusInParts
    local AngleSize = 360 / NumberOfAngles
    
    for r = 1, NumberOfAngles do
        for p = 1, RadiusInParts do
            local Part = GrassPart:Clone()
            -- set part rotation based on r * AngleSize
            -- set part position x and z outwards from character based on p * PART_SIZE based on part rotation

            local Checker = Ray.new(Vector3.new(x, 100, z), Vector3.new(0, -100, 0)) -- make it go from part position down
            local HitPart, HitPosition, HitNormal, HitMaterial = workspace:FindPartOnRayWithIgnoreList(Checker, AllOfYourTreesAndStuff)

            if not HitMaterial or HitMaterial ~= Enum.Material.Grass then -- if it didn't hit anything (no terrain, so no grass) or if the terrain it hit wasn't grass
                -- hide grass
            end
        end
    end
end

On second thoughts, if you have a set number of grass parts, it may be better to keep them and just change their position instead of regenerating them every time the player moves.

And like I said, raycasting. That’s the only way I know of to ‘loop’ through the terrain in a game.

1 Like