Help with understanding how to make part hide behind wall

So i want a part to hide on the other side of the wall so the part can stick to the wall but for some reason it circles it and not sticks near the wall

I want to calculate the length of the red line so i know how much is the length from the center of the wall to the point where the line gets out of the wall.
i have tried numerous ways but they all lead the same way.
here is one of them

local a = workspace.a
local b = workspace.b
local wall = workspace.wall
while wait() do
	local point = wall.Position + Vector3.new(wall.Size.X/2 * (b.Position-a.Position).Unit.X+(((b.Position-a.Position).Unit.X*wall.Size.X/2)),0,wall.Size.Z/2 * (b.Position-a.Position).Unit.Z+(((b.Position-a.Position).Unit.Z*wall.Size.Z/2) ))
--	print((wall.Size/2).Magnitude)
	b.Position = wall.Position +Vector3.new(wall.Size.X*(wall.Position-a.Position).Unit.X,wall.Position.Y,wall.Size.Z*(wall.Position-a.Position).Unit.Z)
	--get b to hide behind the wall

end

and what i get is the part getting circular around the wall
Capture

I’m assuming a is the player and b is the part you’re trying to hide from the player?

You can calculate the point closest to the player on the edge of the wall and the calculate the distance from that point to the center of the wall.

local center = wall.Position
local x = math.clamp(a.Position.X, center.X - wall.Size.X / 2, centerX + wall.Size.X / 2)
local z = math.clamp(a.Position.Z, center.Z - wall.Size.Z / 2, centerZ + wall.Size.Z / 2)

-- This is the point closest to the player on the edge of the wall in the xz plane
local point = Vector3.new(x, 0, z)
local centerToPoint = point - center

-- Find the distance
local distance = centerToPoint.Magnitude

-- Move the part to the opposite side (You're going to need to account for the part's size)
-- local Offset = Some offset vector to account for the part's size
a.Position = center - centerToPoint + Offset

Yeah that is what i got when i used on the part, but what i experience is when i rotate the wall the part is still moving to the old wall’s position. that is the issue i try to solve
this works on a wall that has 0 rotation:
Capture2
but when i rotate the wall the part doesnt register it and it doesnt follow the wall:
Capture

Ahh… That’s probably because the positions are in the world coordinate frame and they’d need to be in the wall’s coordinate frame.

What if you do something like this at the beginning?

-- Not sure if you care about the entire CFrame being transformed or just the positions
local newACFrame, newBCFrame= wall.CFrame:ToObjectSpace(a.CFrame, b.CFrame)

Then do the same math you did before but with newACFrame.Position / newBCFrame.Position instead of a.Position / b.Position

Its not working also i dont understand what it means. i mean my code is:

local a = workspace.a
local b = workspace.b
local wall = workspace.wall
while wait() do
	b.Position = wall.CFrame * wall.CFrame:VectorToObjectSpace(Vector3.new(math.clamp(wall.Position.X-a.Position.X,-wall.Size.X/2,wall.Size.X/2), 0, math.clamp(wall.Position.Z-a.Position.Z,-wall.Size.Z/2,wall.Size.Z/2))) 
end


but it still doesnt work on that for some reason

Try this:

local a = workspace.a
local b = workspace.b
local wall = workspace.wall
while task.wait() do
	local posA = wall.CFrame:PointToObjectSpace(a.Position)
	
	-- This is the point closest to the player on the edge of the wall in the xz plane
	local x = math.clamp(posA.X, -wall.Size.X / 2, wall.Size.X / 2)
	local z = math.clamp(posA.Z, -wall.Size.Z / 2, wall.Size.Z / 2)
	local edgePoint = Vector3.new(x, 0, z)
	
	-- We need to trasnform the point back to the world coordinate frame
	local posB = -edgePoint
	b.Position = wall.CFrame:PointToWorldSpace(posB)
end

I forgot to mention in my last comment that we’d need to transform the calculated position back to the world coordinate frame.

1 Like

now i see, it needs to be transformed to get the coordinate so then we can translate it to the world place to make it relative to the wall rotation. thanks that helped a lot

1 Like

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