Trying to Raycast the pet right above the ground. The Pet still falls through the ground

	ConnectedPets = game["Run Service"].Heartbeat:Connect(function()
		for i, Pet in equippedPetsChar:GetChildren() do		
			
			if Pet.PrimaryPart and char and char.HumanoidRootPart then
				local CharacterPosition = char.HumanoidRootPart.Position
				local DesiredPosition = (char.HumanoidRootPart.CFrame*CFrame.new(0,0,4)).Position

				local ActualPosition = workspace:Raycast(
					DesiredPosition + Vector3.new(0, 20, 0), 
					Vector3.new(0, -50, 0), 
					raycastParams
				)
				
				if ActualPosition then 
					Pet.PrimaryPart.BodyPosition.Position = ActualPosition.Position + Vector3.new(0, math.max(Pet.Y.Value/1.9), 0)
				else 
					Pet.PrimaryPart.BodyPosition.Position = DesiredPosition
				end
				
				Pet.PrimaryPart.BodyGyro.CFrame = CFrame.lookAt(Pet.PrimaryPart.Position, CharacterPosition)
			end
		end
		task.wait()
	end)

2 Likes

First we need to know if the problem is with the Raycast, add this

print(ActualPosition and ActualPosition.Position and ActualPosition.Position.Y)

to the code and see if the Raycast is alright.

also, what is the point of looping through all pets in a equippedPetsChar with this code? since the only input from the Pet itself is at the end, shouldn’t the script be organized like this?:

    ConnectedPets = game["Run Service"].Heartbeat:Connect(function()

		if not char or not char.HumanoidRootPart then return end
		
		local CharacterPosition = char.HumanoidRootPart.Position
		local DesiredPosition = (char.HumanoidRootPart.CFrame*CFrame.new(0,0,4)).Position

		local ActualPosition = workspace:Raycast(
			DesiredPosition + Vector3.new(0, 20, 0), 
			Vector3.new(0, -50, 0), 
			raycastParams
		)

		for i, Pet in equippedPetsChar:GetChildren() do	if not Pet.PrimaryPart then continue end
		
				if ActualPosition then 
					Pet.PrimaryPart.BodyPosition.Position = ActualPosition.Position + Vector3.new(0, math.max(Pet.Y.Value/1.9), 0)
				else 
					Pet.PrimaryPart.BodyPosition.Position = DesiredPosition
				end
				
				Pet.PrimaryPart.BodyGyro.CFrame = CFrame.lookAt(Pet.PrimaryPart.Position, CharacterPosition)
			
		end
		task.wait()
	end)

this way you can do a single raycast for all of them, instead of re-doing multiple raycasts wiith same origin and direction within the same frame, which all result in the same value anyway.

i also, i don’t get the point of having the task.wait() at the end, isn’t this Heartbeat? why would it wait?

1 Like

The wait and for loop in equippedpetschar were segments of code from when I originally had this in a while loop. I was going to change the X and Z positions for each pet in the folder, based on the number equipped and if they’re flying pets, so the raycast wouldn’t be the same for all of them, but I forgot about the wait, i was planning to clean up the code after fixing this bug. Here’s the output for the print

How high/low is Y 1.3 relative to your ground?

also, Is the “canQuery” property of your ground enabled?

change up the ‘Print’ to be

print(ActualPosition and ActualPosition.Position and ActualPosition.Position.Y,ActualPosition and ActualPosition.Instance)

so we can see which Object exactly is being hit by the Raycast. (no need to post a video tho, you can just check if it’s the ground that is being hit by it by clicking on it’s Name on the console, when printing an instance directly on console you can go to it with a click)


I checked, and some of the ground doesn’t even have a canQuery property, here’s output.

Also, CanQuery returns as true when I print it, it doesn’t return as false even when the pet falls beneath the ground

canQuery is automatically enabled if canTouch is enabled, so unless you disable it, it’s a hidden property.

but from your last video… isn’t it working? i’m not seeing anything fall through.

It’s not working in the last video, I just didn’t show replication of the bug in that 3 second clip. 2 seconds later it falls beneath the ground, here


.

is it also falling bellow ground on server view or is it a client-sided problem?


Hmm, do you reckon that’s the issue? Sometimes the network ownership isn’t entirely in the client’s control, although there’s not enough size for me to clip the whole video for you. Do you think it’s not a faulty raycast issue then?

also the pets are above the ground substantially in that clip because I enabled them to fly while debugging.

i mean, the raycast itself seems to be working pretty well based on the Y readings we saw before, i’ve already moved away from the possibility of it being the problem to it being related to how the Movers work.

if you anchor the pet and set the pet’s position directly instead of the target position for the mover, you should see (i hope so, lol) that the position is as it should be, which would prove the problem is with the physics surrounding the act of making it move using the BodyPosition, not the position being given.

if my test is right, maybe you should consider switching from BodyPosition (which is deprecated btw) to AlignPosition (which i think it’s kinda worse to work with, but works better in the end).

1 Like

Yeah, I think you’re right, so I was doing align position to begin with and was thinking of transitioning back to it, but then how do I precisely set the pets at ground level with align position? I tried raycasting the pets with align position right at the ground, but it didn’t work at all, that was why i switched to bodymovers.

The Raycast result is the desired ground level, the only thing you need to add is the Y offset which should be half of the Pet’s Height.

unless i misunderstood your question, that should be it, it works very similarly to the BodyPosition you’re using, just with less bugs, specially replication ones.

(which is actually the same as an Humanoid’s HipHeight, the system Roblox uses to that Humanoids collide with the ground without actually colliding with it, too bad it’s an exclusive function of Humanoid).

1 Like

Oh wait, yeah I think you’re right lmao, my brain is fried man

1 Like

same for me, lol, imma go sleep now, good luck.

1 Like

Was able to do it without switching to AlignPosition. Had to increase my max force to 6000.


1 Like

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