Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why isn't my ray casting script working? trying to make model not go through parts

I am making a butterfly that flyie around randomly. I'm wanting to make it so it doesn't fly through parts. Someone recommended using raycasting, however, by script isn't working. The butterfly won't turn around when its gets near parts.

local R =  Ray.new(Part.Position, Vector3.new(50,0,0))

local Butterfly = script.Parent.Parent -- The butterfly

local LWing = Butterfly.LWing -- The butterfly's wings
local RWing = Butterfly.RWing

local Body = script.Parent.Parent.PrimaryPart -- The primary part of the butterfly so I can use the CFrame

while true do
	wait()
	    local PartFound, Pos = workspace:FindPartOnRayWithIgnoreList(R, { -- The ignore list is the butterflys body parts, I'm usnure how to shorten it but that doesn't have anything to do with my question
		Butterfly.Torso1, Butterfly.ConfirmedButterfly, 
		Butterfly.Head, Butterfly.LeftAntenna, 
		Butterfly.RightAntenna, RWing.Center, 
		RWing.Hinge,
		RWing.Part1, RWing.Part2,
		RWing.Part3, RWing.Part4,
		RWing.Part5, RWing.Part6,
		RWing.Wedge1, RWing.Wedge2,
		RWing.Wedge3, RWing.Wedge4, 
		LWing.Center, LWing.Hinge,
		LWing.Part1, LWing.Part2,
		LWing.Part3, LWing.Part4,
		LWing.Part5, LWing.Part6,
		LWing.Wedge1, LWing.Wedge2,
		LWing.Wedge3, LWing.Wedge4})
	script.Parent.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.Angles(math.rad(math.random(-5,5)), math.rad(math.random(-5,5)), math.rad(math.random(-5,5))))		
			wait()
	script.Parent.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.new(1,0,0)) -- Makes the butterfly rotate in a random direction then go in that direction
	if PartFound then -- Checks if a part was found by the Ray
		if (PartFound.Position - Pos).Magnitude <= 5 then -- Checks the distance between the part and the butterfly
			script.Parent.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.Angles(math.rad(0), math.rad(180),math.rad(0))) -- Makes the butterfly turn around
			script.Parent.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.new(3,0,0)) -- Makes the butterfly move away
    	end
	end
end

I don't understand why the butterfly isn't turning when it gets near parts. Any help would be appreciated. Thank you in advance.

1 answer

You raycast only once, in a direction that might not be where the butterfly is moving. The second argument in the ray should be the lookVector/unitray of the CFrame where the butterfly will be moving and multiply that by 100. This means that the movement must be saved before the ray is created.

while true do
	local CFrameToMoveTo = CFrame.new(math.random(-5,5),math.random(-5,5),math.random(-5,5)
	local RayN = Ray.new(Part.Position, CFrameToMoveTo.LookVector * 100)
	local Part, Pos = workspace:FindPartOnRay(RayN, Model, false, true) -- just set the ignore to the model and it will ignore the whole butterfly.
	if Part and (Position - PrimaryPart.Position).Magnitude < 5 then
		-- rotate and move away
	else
		-- move by CFrame
end

2 comments

0

The butterfly still seems to fly through everything

0

Try reducing the distances by which it moves.

This discussion is preserved as it was. New answers, comments, and votes are closed.