Shapecasts Unable To Detect Objects

I have a gorilla model that will be able to climb trees. To achieve this when the gorilla touches parts tagged as “Trunk” a prismatic constraint will be setup allowing the player to climb up and down. This is determined with a .TouchedEvent and then using a Shapecast to ensure the character is actually looking at the tree.

I do this over trusses because the gorilla scales in the game over time and eventually trusses don’t get detected as climbable once you get to a certain size

while task.wait() do
   if TouchingTrunk and not Climbing then
   	if TouchingTrunk:HasTag("Trunk") then
   		local Params =  RaycastParams.new()
   		Params.FilterDescendantsInstances = {script.Parent, game.Workspace.Terrain}
   		Params.FilterType = Enum.RaycastFilterType.Exclude
   		Params.IgnoreWater = true
   
   		local Shapecast = game.Workspace:Spherecast(RaycastAttachment.WorldPosition, 4, RaycastAttachment.WorldCFrame.LookVector * 7, Params)
   		if Shapecast then
   			warn("Shapecasted") -- Shapecast detected an object
   			print(Shapecast.Instance)
   			if Climbing == false and Shapecast.Instance == TouchingTrunk then
   				Climbing = true
   				task.spawn(BeginClimbing, TouchingTrunk)
   			end
   		else
   			print("Shapecast Failed") -- Shapecast failed at detecting an object
   		end
   	end
   	task.wait(0.1)
   end
end

When the character attempts to jump it will destroy the prismatic constraint and push the character back. The issue is if the character is still touching the tree when it slides down, the shapecast detecting a touch doesn’t register.

So when I first go up to the tree it prints "Shapecasted", but when I jump and move forward to “hug the tree” it repeatedly prints "Shapecast Failed" implying the shapecast doesn’t seem to pick up the tree trunk, until you walk away and go towards it again.

I’m pretty stumped with this one so I figured I would make a devforum post, feel free to ask for any more information :slight_smile:

Are you doing this on the server or on the client?

I’m doing this on the server.

Also when I mean “walk away and walk back towards” I mean, once the end of the shapecast stops touching the part.

Right now my hackish solution is just to make the distance of the shapecast really short, so animations alone will naturally push away the character enough for it to reset/detect the shapecast again. The issue can still happen if you intentionally hug the tree though.

this may be related characterlimitwithquoteswhat

1 Like

Well it doesn’t have anything to do with climbing as the Shapecast clearly fires, the printing is just checking wether or not it actually hit something.

Hmm do you think if I changed the distance of the shapecast in the frame before and then reset it back, it will fix the issue?

welp that worked, really goofy though

Solution:
Fire a 2nd shapecast with a really short distance before you fire the real one so Roblox doesn’t detect it as a “part it’s already colliding with”

Updated Code:

while task.wait() do
	if TouchingTrunk and not Climbing then
		print(TouchingTrunk)
		if TouchingTrunk:HasTag("Trunk") then
			print(Climbing)

			local Shapecast = game.Workspace:Spherecast(RaycastAttachment.WorldPosition, 2, RaycastAttachment.WorldCFrame.LookVector * 3.5, Params)
			if Shapecast then
				warn("Shapecasted")
				print(Shapecast.Instance)
				if Climbing == false and Shapecast.Instance == TouchingTrunk then
					Climbing = true
					task.spawn(BeginClimbing, TouchingTrunk)
				end
			else
				print("Shapecast Failed")
			end
		end
		task.wait(0.1)
		local Shapecast = game.Workspace:Spherecast(RaycastAttachment.WorldPosition, 2, RaycastAttachment.WorldCFrame.LookVector, Params) -- To fix collision issue with shapecasts
	end
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.