How can I position a part at the end of another part?

Hey, Im trying to create a grabbing beam; where if it hits a part named "Target’ it grabs the part.

My issue: it positions the part at the near middle of the prototype beam, instead of at the end.
How can I fix this?

position

Code:

--//Variables---

local tool = script.Parent
local origin = tool:WaitForChild("Model").Origin
local laserFolder = game.Workspace.Laser
local run = game:GetService("RunService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local range = 50

mouse.TargetFilter = laserFolder

--//Loop---
run.RenderStepped:Connect(function()
	
	local mousePosition = mouse.Hit.p
    local originPosition = origin.Position	

	
	local direction = (mousePosition - originPosition).Unit * range
	local result = workspace:Raycast(originPosition,direction )
	local beam = game.Workspace.Laser:FindFirstChild("Beam")
	local midpoint = originPosition + direction/2
	
	beam.CFrame = CFrame.new(midpoint,originPosition)
	beam.Size = Vector3.new(.5,.5, direction.Magnitude)

	if result then
		if result.Instance.Name == "Target" then
			if not beam:FindFirstChild("WeldConstraint") then
			 local weld = Instance.new("WeldConstraint")
			 weld.Parent = beam
			 weld.Part0 = result.Instance
			 weld.Part1 = beam
			 end
			---Im having the issue here--
            result.Instance.CFrame = beam.CFrame
		end
	end	
end)

I get this error:

sorry I meant size. I am testing it rn to try to get it to work

1 Like

I was able to get the equation

local resultInstance = result.Instance
local beamCoordinate = beam.CFrame
resultInstance.CFrame = beamCoordinate * CFrame.new(Vector3.new(0, beamCoordinate.Position.Y / 2 - resultInstance.Size.Y / 2, 0))

to work B)

depending on the object, you may have to use a different axis than Y

This it what happens: it moves to another postion everytime I interact with it
https://gyazo.com/f92f75e57bf676255fe70cbd9a2f1da2

uhhhh maybe try replacing the axis with a different one?

resultInstance.CFrame = beamCoordinate * CFrame.new(Vector3.new(0, beamCoordinate.Position.X / 2 - resultInstance.Size.Y / 2, 0))

-- or

resultInstance.CFrame = beamCoordinate * CFrame.new(Vector3.new(0, beamCoordinate.Position.Z / 2 - resultInstance.Size.Y / 2, 0))

since I do not know which axis is the longest on your object. I don’t know what else would be causing this to happen

Im using the Z axis on my beam

ah so does

local resultInstance = result.Instance
local beamCoordinate = beam.CFrame
resultInstance.CFrame = beamCoordinate * CFrame.new(Vector3.new(0, beamCoordinate.Position.Z / 2 - resultInstance.Size.Y / 2, 0))

work?

The target doesnt stick to the beam, instead it just moves away

To do that you need to divide the beam’s size by 2 and after that set the parts cframe to the beam cframe + the beam size divided by 2.

After replicating your code, I got

resultInstance.CFrame = beamCoordinate * CFrame.new(0, 0, beam.Size.Z / 2 - resultInstance.Size.Z / 2)

to work! Cheers

1 Like

You could omit the Vector3 constructor from this.

1 Like