Raycast Position

Howdy y’all. I’m terrible at math. Could someone explain to me how to achieve cframing the blue block to be flush with the wall after the ray returns a hit result?

local CollectionService = game:GetService('CollectionService')

local target = workspace.Target
local bot = workspace.Bot

function castRay(direction)
	local origin = bot:GetAttribute('Position') or bot:GetPivot()

	local targetDirection = (direction.Position - origin.Position).Unit
	local directionVector = targetDirection * 10

	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {bot}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	
	local raycastResult = workspace:Raycast(origin.Position,directionVector,raycastParams)

	local hitPosition = (raycastResult and raycastResult.Position) or (origin.Position + directionVector)

	return raycastResult,hitPosition
end

while task.wait(.5) do
	local raycastResult,hitPosition = castRay(bot:GetPivot() * CFrame.new(0,0,1))
	
	if not raycastResult then
		bot.CollisionPart.Position = hitPosition
	end
end

Goal:

1 Like

Try this maybe:

-- variables
local part: BasePart = script.Parent

-- functions
local function Raycast(origin: Vector3, direction: Vector3)
	-- declare the raycast params
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {part}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	-- shoot a raycast from the origin point towards the direction, given the raycastParams variable
	local raycast: RaycastResult = workspace:Raycast(origin, direction, raycastParams)

	-- if a raycast exists then return the position
	if (raycast) then
		return raycast.Position
	end
end

-- use the raycast function to retrieve the position of the raycast
local raycastPosition: Vector3 = Raycast(part.Position, part.CFrame.LookVector * 100)

-- check if the position exists
if (raycastPosition) then
	-- set the parts position to be against the wall (aka the raycasts position)
	part.Position = raycastPosition - part.CFrame.LookVector * (part.Size.X / 2) - (Vector3.xAxis / 2)
end

The code is quite barebones but it works for the X axis. Or rather, it works when the part faces the correct direction ( forward ). Hope this helps

ServerScriptService.BotService:27: attempt to perform arithmetic (sub) on Vector3 and number

Getting that error on this line.

part.Position = raycastPosition - part.CFrame.LookVector * (part.Size.X / 2) - 0.5
1 Like

Pardon that error, try this instead:

-- variables
local part: BasePart = script.Parent

-- functions
local function Raycast(origin: Vector3, direction: Vector3)
	-- declare the raycast params
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {part}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	-- shoot a raycast from the origin point towards the direction, given the raycastParams variable
	local raycast: RaycastResult = workspace:Raycast(origin, direction, raycastParams)

	-- if a raycast exists then return the position
	if (raycast) then
		return raycast.Position
	end
end

-- use the raycast function to retrieve the position of the raycast
local raycastPosition: Vector3 = Raycast(part.Position, part.CFrame.LookVector * 100)

-- check if the position exists
if (raycastPosition) then
	-- set the parts position to be against the wall (aka the raycasts position)
	part.Position = raycastPosition - part.CFrame.LookVector * (part.Size.X / 2) + (Vector3.xAxis / 2)
end
1 Like

Thank you! Not exactly what I’m looking for but I believe you showed me the math that is needed.

Thanks!

1 Like

BTW, Raycast is deprecated. you should use Workspace:Raycast() instead

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