Raycasting from a custom rig returns nil while raycasting from a dummy returns a part?

Hey, I’m currently trying to expand my knowledge on ai and pathfinding related stuff but I ran into a problem. I’m trying to make it so that the AI uses raycasting to check for obstacles while following a target. If there are no obstacles then it’ll just use :MoveTo() and if there are any then it’ll use pathfinding.

local function Raycast_Target(target)
	local rayOrigin = Monsterr.Head.Position
	local rayDirection = MonsterrHRP.Position - target.HumanoidRootPart.Position

	local rayargs = RaycastParams.new()
	rayargs.FilterDescendantsInstances = {Monsterr,target}
	rayargs.FilterType = Enum.RaycastFilterType.Exclude

	local newray = workspace:Raycast(rayOrigin,rayDirection,rayargs)

	local distance = rayDirection.Magnitude

	print("RESULT: ",newray)

	return newray
end

The function above works when I put the script in a default dummy but it stops working when I put it in a custom rig. Any idea why? The properties are the same for all of the limbs and the script is identical for both the monsters.

(green is the dummy’s raycast and white is the monster’s raycast, green returns the gray parts while white does not)

3 Likes

I just read through the devforum and apparently I should’ve used RayDirection.Unit which didn’t help either and the raycast still returns nil.

1 Like

I can see a few issues:

  1. Your rayDirection is inverse. In vector math, when you want to create a vector AB which points from A to B, use this formula AB = B - A.
  2. You’re excluding (which also means to ignore) the target from the raycast operation when adding it to the FilterDescendantsInstances.

Hope this helps! :blush:

3 Likes

So you’re saying I should do target.HRP.Pos - target.HRP.Pos?

And also I’m blacklisting the target because i want the raycast to only hit walls and terrain to look for obstacles while following the target. If it’s able to hit the target it’ll always use pathfinding - even though now that I think about it, it doesn’t seem that bad. I suppose I could give up on the raycast thing.

Ah, gotcha! Think of it like this rayDirection = targetPosition - rayOrigin.

why are u excluding the target on RaycastParams

rayargs.FilterDescendantsInstances = {Monsterr,target}

Please read the entire post and read what I said above before replying

I’m excluding the target because I want to use the raycast to check for obstacles between the target and the part. If there aren’t any it’d use MoveTo and if there are any it’d use pathfinding. If the target wasn’t blacklisted it would’ve always returned a part which means it would always use pathfinding

I rewrote the raycast function and triple checked every property and double read every raycast related documentation and it still doesn’t work for the custom rig.

The dummy is pathfinding with 0 problems while the custom rig fails the raycast right away. I cast both the rays from a humanoidrootpart and both the scripts and the limbs properties are the same.

1 Like

Turns out the ray was being casted backwards for whatever reason.

1 Like

This is what I said over 6 hours ago :blush:

This should be marked as the answer:

2 Likes

No it should not as the ray was being casted forward for the dummy. The issue was not the script, it was the rig. If I did what you told me to and ever switched the rig to a different one it’d break again.

You’re wrong :blush: The way your rig/dummy was configured couldn’t have affected the result because you’re not taking orientation into account anywhere in your code. Your code is the issue, not the model.

The rayDirection in your initial code is inverse, which means the ray will cast the opposite direction of what you intended.

I didn’t change the code whatsoever, I only rotated the humanoid root part by 180 degrees and it fixed the issue.

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