Raycast not detecting collision even though im casting it in the direction of a part

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want to ray cast to a surface point of a model that I have. Basically, the model is an island which has a marker on top that is used for things such as the players nametag, I am also using this as the origin for my ray.

  1. What is the issue? Include screenshots / videos if possible!

The issue is that the raycast hardly ever actually successfully detects a part even though how I am casting the ray should always contact a part.

At the moment my code determines a random part of the model and raycasts towards it, so at least the ray should collide with that part or any covering it.

The marker that is the origin, is 10 studs away from the island and every part of the island is within range considering the 25 distance.

function ReturnRandomSurfacePointOfIsland(island)
	local raycastResults
	local direction = (island.Island:GetChildren()[math.random(1, #island.Island:GetChildren())].Position - island.PrimaryPart.Position).Unit
	print(direction)
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Include
		params.FilterDescendantsInstances = {island.Island}
	raycastResults = workspace:Raycast(island.PrimaryPart.Position, direction * 25, params)
	print(raycastResults)
	return raycastResults
end

Thanks

function ReturnRandomSurfacePointOfIsland(island)
	local raycastResults
	
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Include
	params.FilterDescendantsInstances = {island.Island}
	local part = island.Island:GetChildren()[math.random(1, #island.Island:GetChildren())]
	
	raycastResults = workspace:Raycast(island.PrimaryPart.Position, (part.Position - island.PrimaryPart.Position) * 100, params)
	
	return raycastResults
end

I have tried this which seems no different

dont multiply the direction by 100

if you want it to shoot from origin then you use multiply

but if you are shooting towards another object then just do

(origin, target - origin, params)

1 Like

isnt it target - origin instead of origin - target, that would just cast a direction from target to origin

yes sorry it should be target - origin

just to be triple sure, CanQuery is set to true, right?

there is still no contact with a descendant of island.Island

Try excluding the origins part/model. Also, what is the output of the raycast result, is it nil or is there a hit? And to add on T3_MasterGamer’s suggestion, is CanCollide also turned on? Lastly check if there are any anything that affects the raycast result (e.g. parts in the way, changing the variable to something else, etc…)

If nothing else works try creating a part and raycasting to it just to make sure that the raycast is actually working

For future reference, I had neglected to set the CollisionGroup of the RayCast Parameters to the relevant group.

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