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
-- 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
-- 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