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

How do I cast a ray towards a specified part?

Sorry for spamming the site but I'm close to where I want this game to be at the moment. I want to test if the gun is clipping through a wall. The way I want to go about this is casting a ray from the tip of the gun towards the head of the character. If it intersects a part (so if the gun is clipping something) it won't fire the gun. This is what I tried for the sake of trying (pls dont bully me JesseSong)

function CheckCollision()
	local rayOrigin = FirePointObject.Position
	local rayDirection = script.Parent.Parent.Head
	
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {script.Parent} -- gun
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
	if raycastResult then 
		return
	end
end

I get an error saying Unable to cast Instance to Vector3 so I probably got the rayDirection wrong. Any help is appreciated.

1 comment

0

You also need to multiply that direction by the range of your gun in studs.

1 answer

✓ Accepted answer

Your RayDirection is the Instance. But, the ray direction must be a vector3 value.

Simply set the rayDirection to script.Parent.Parent.Head.Position. (which is a vector3)

You were previously setting it to the Head, which is not a Position.

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