Raycast ignores baseplate ?!

please only give answers you have tried in the past, and no CHATGPT responses - please.

So, we want to drop a landmine to the ground. We are using a raycast to “find” the ground so we can set the landmine’s CFrame. However, the Raycast isn’t finding anything when it shoots through the baseplate. The baseplate is not blacklist/excluded. Here’s our code if it helps:

local tool = script.Parent
local replicatedS = game:GetService("ReplicatedStorage")

local function onActivated()
	local deployedMineClone = replicatedS:WaitForChild("Landmine"):Clone()

	local rayOrigin = tool.Handle.Position--Vector3.new(hrp.CFrame)

	--local rayOrigin = Vector3.new(hrp.CFrame)
	local rayDirection = Vector3.new(0, -100, 0)

	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	raycastParams.FilterDescendantsInstances = {script.Parent.Parent}
	raycastParams.IgnoreWater = true
	--local raycastResult = workspace:Raycast(rayOrigin, (rayDirection).Unit * 10, raycastParams)
	local raycastResult = workspace:Raycast(rayOrigin,rayDirection, raycastParams)
	    --this function takes the ray as a parameter. Below, it gets the midpoint.
		local MidPoint = tool.Handle.Position + (tool.Handle.Position +  Vector3.new(0, -100, 0))/2

	    --below it creates the part and returns it. I'm sure you're familiar with properties
		local Part = Instance.new("Part")
		Part.Parent = workspace

		Part.Anchored = true
		--Part.CFrame = CFrame.lookAt(MidPoint, tool.Handle.Position)
		Part.CFrame = CFrame.lookAt(MidPoint, tool.Handle.Position)
		Part.Size = Vector3.new(1, 1, 100)	
		if raycastResult then
		print(raycastResult.Instance.Position)
	--
		deployedMineClone:SetPrimaryPartCFrame(raycastResult.Instance) --you can also edit the orientation though the second parameter of the CFrame
		deployedMineClone.Parent = game.Workspace
		deployedMineClone.Top.CanTouch = false
		deployedMineClone.Mid.CanTouch = false
		deployedMineClone.Bottom.CanTouch = false
		wait(2)
		deployedMineClone.Top.CanTouch = true
		deployedMineClone.Mid.CanTouch = true
		deployedMineClone.Bottom.CanTouch = true
		deployedMineClone.Top.Anchored = true
		deployedMineClone.Mid.Anchored = true
		deployedMineClone.Bottom.Anchored = true
		--tool:Destroy()
	end
end

tool.Activated:Connect(onActivated)

edit:

this code is in the folder tree like this:

workspace/
landmine tool (a tool object)/
DeployMineServerScript


also, we’ve had lots of success with raycast in the past - we have bots that will shoot at your character and stuff that we worked on and adjusted. I just can’t get it to shoot straight down and see the baseplate.

Kind thanks !

You use print, so where and what is the script printing? This code is very unorganized and I can’t tell what is going on. function RayToPart is also unused.

1 Like

thank you for your response!

typically, when folks need help, their code is a bit disorganized.

function raytopart is not called, but we use it to show where the ray ends up, when it is called. I removed it since it is confusing you.

i also removed the reference to humanoidrootpart, since we aren’t using it anymore, and it is confusing you.

the print statement returns “attempt to print nil” or whatever the wording for that is - because it finds nothing :slight_smile:

can anyone who can understand the code above please lend a hand.

thank you for your response, it is appreciated ! :slight_smile:

I tried fixing a few things, but obviously I can’t test it.

local tool = script.Parent
local storage = game.ReplicatedStorage

tool.Activated:Connect(function()
    local char: Model = script.Parent.Parent
    local deployment = storage.Landmine:Clone()

	local origin = char.PrimaryPart.Position
	local direction = Vector3.new(0, -250, 0)
    
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Exclude
	params.FilterDescendantsInstances = {char}
	params.IgnoreWater = true

    local result = workspace:Raycast(origin, direction, params)

    local MidPoint = origin + direction / 2

    local Part = Instance.new("Part")
    Part.Anchored = true
    Part.CFrame = CFrame.lookAt(MidPoint, tool.Handle.Position)
    Part.Size = Vector3.new(1, 1, math.abs(direction.Y))	
    Part.Parent = workspace

    if result then
		print(result, result.Instance.Position)
        
		deployment:SetPrimaryPartCFrame(result.Instance.CFrame)
		deployment.Top.CanTouch = false
		deployment.Mid.CanTouch = false
		deployment.Bottom.CanTouch = false
        deployment.Parent = workspace
		task.wait(2)
		deployment.Top.CanTouch = true
		deployment.Mid.CanTouch = true
		deployment.Bottom.CanTouch = true
		deployment.Top.Anchored = true
		deployment.Mid.Anchored = true
		deployment.Bottom.Anchored = true
		--tool:Destroy()
	end
end)
1 Like

thank you VERY MUCH for trying to help. let me give this a shot !

IT WORKS. Thank you very much - we were working on this for an entire week or so I think.

I am going back to study what you did now so I can use this better. We’ve been coding a while, and really appreciate when someone is kind enough to help us with the few parts we do not understand ~! So thank you so much !

I tested it myself and the code works in a new place

wait(10)

local rayOrigin = game.Players.LocalPlayer.Character.PrimaryPart.Position--Vector3.new(hrp.CFrame)

--local rayOrigin = Vector3.new(hrp.CFrame)
local rayDirection = Vector3.new(0, -100, 0)

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}
raycastParams.IgnoreWater = true
--local raycastResult = workspace:Raycast(rayOrigin, (rayDirection).Unit * 10, raycastParams)
local raycastResult = workspace:Raycast(rayOrigin,rayDirection, raycastParams)

print(raycastResult.Instance)

the only suggestion i can give is making a visualization of the raycast in workspace so you can see whats going on on your end

I basically just optimized it, I didn’t really know what was wrong. I guess the issue was hidden is some parts of the code?

2 Likes

Thank you very much for your response ! Blueberry already fixed it, but we really appreciate your help

We do make a visualization of the part that is recast, if you look at the code above.

We really appreciate the response :slight_smile:

I’m going to compare and find out. and see how you optimized it. we are still working on elegance. the code above was especially messy because it wasn’t in its final form. a lot of time our code goes that way. working on it ! You rule !

Oh, so thats what that is. What about using a LineHandleAdornment for that?

1 Like

I’ll have to learn what that is :slight_smile: Sometimes we have trouble keeping up with options because we code/build so much we don’t have time to see the new stuff…even when its not that new (been at this for about 4 or 5 years now)

thanks !

1 Like

OK, some differences I see in your code as we study it, but I’m not sure why it works like this, instead of as we did it. It seems you kind of do the same thing…but ours fails. Some differences we note:

  1. for the origin, you use the character primarypart.position in stead of humanoidRootPart.position or the tools handle.position

  2. you reference the character in a way I don’t totally undersatnd, as
    local char: Model = script.Parent.Parent

  3. your direction is vector3.new(0,-250,0) whereas ours used -100

  4. you use the cframe of the result from the raycast, instead of just its instance - noted - even though this wasn’t causing our fail since it was inside the if statement… but thanks, that was an error for sure.

any reason you do item #2 this way? – char: Model = script.parent.Parent… I’m not used to the : Model = ?

Hey strange thing - It’s working better, but for some reason it’s putting the cloned landmine really close to the origin. No matter where we activate the tool in 3d space.

stranger still - the cloned Mine doesn’t have original position at origin, so that’s not the reason… we are still working on it, but really feel you have us close to an answer.


Ok just figured it out. We are using
result.cframe - it hits the baseplate. and the baseplates position is at 0,0,0 so it puts our landmine at the center of the baseplate, not where we touched it. Got it.

It’s the same way you referenced it, I just put it in a variable!

The : Model is simplying just adding a type to the variable. This will give you autocomplete so it’s easier to code. Without that, it give you autocomplete for the baseclass Instance.

Change line:

deployment:SetPrimaryPartCFrame(result.Instance.CFrame)

To:

deployment:PivotTo(CFrame.lookAt(result.Position, result.Position + result.Normal))
1 Like

We had just changed it to:
deployment:SetPrimaryPartCFrame(CFrame.new(result.Postion))

before reading your response. we will try what you use now! (our idea didn’t work, so lets try yours.)


Just tried it, and it works ! I’ll have to learn about this now (pivotTo) too. Kind thanks for the link to Typechecking. If more folks on this forum were as helpful as you, it wouldn’t have taken us 4 years to learn what we’ve learned.

1 Like

PivotTo does the same thing as SetPrimaryPartCFrame, except the Pivot can be changed. The default pivot is the primary part cframe or the cframe of the model bounding box.

1 Like

thank you. is it ok if I DM you sometime if I have a question? you don’t have to respond (of course) but help is appreciated.

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