Hook not following moving objects

I’m trying to make a grappling hook system, which works pretty well when there’s no interaction with moving objects, however I noticed if there is a moving object, it doesn’t follow it.

I’ve tried using Welds and WeldConstraints to follow the moving object, however it doesn’t work as nicely as I would have hoped. The Weld instance anchors the hook to the centre of the object, and the WeldConstraint doesn’t follow the object at all.

This has introduced a whole host of other issues such as the player not looking toward where the hook is going and flying around.

CastHook.Fire = function(player,side,pos,target)
	local char = player.Character
	local ODM = char.ODMGear
	local Barrel = nil
	local hook = nil

	if side == "Left" then
		if ODM.Propeller.LeftBarrel.Hook:FindFirstChild"retract" then return end
		Barrel = ODM.Propeller.LeftBarrel
		hook = Barrel.Hook
		Barrel:SetAttribute("Engaged",true)
		
	elseif side == "Right" then
		if ODM.Propeller.RightBarrel.Hook:FindFirstChild"retract" then return end
		Barrel = ODM.Propeller.RightBarrel
		hook = Barrel.Hook
		Barrel:SetAttribute("Engaged",true)
	end
	
	local t = 2*pos.Magnitude/2000
	local info = TweenInfo.new(t,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut,0,false,0)
	local tweenLength = TS:Create(Barrel.Wire,info,{Length = pos.Magnitude})
	local tweenPosition =TS:Create(hook,info,{Position = pos})
	sounds.ODMSounds:Play()

	tweenLength:Play()
	tweenPosition:Play()
	hook.Anchored = true

	tweenPosition.Completed:Wait()
	sounds.GasApplied:Play()

	local weld = Instance.new("WeldConstraint",hook); weld.Name = "Weld"; weld.Part0 = hook; weld.Part1 = target
	hook.Anchored = false

	local info_Speed = TweenInfo.new(1,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut,0,false,0.1)
	Barrel.BarrelForce.Enabled = true
	local speedTween = TS:Create(Barrel.BarrelForce,info,{VectorVelocity = Vector3.new(100,0,0)})
	speedTween:Play()


	accelerating = true
	speedTween.Completed:Connect(function()
		sounds.Reel:Play()
		accelerating = false
	end)


	if not char.LowerTorso:FindFirstChild"AlignVertical" then
		alignY = Instance.new("AlignOrientation")
		alignY.Mode = Enum.OrientationAlignmentMode.OneAttachment
		alignY.Parent = char.LowerTorso
		alignY.Attachment0 = char.LowerTorso.WaistRigAttachment
		alignY.Responsiveness = 5
		alignY.Name = "AlignVertical"
	end


	if not char.LowerTorso:FindFirstChild"AlignLook" then
		o = Instance.new("AlignOrientation")
		o.Parent = char.LowerTorso
		o.Mode = Enum.OrientationAlignmentMode.OneAttachment
		o.Attachment0 = char.LowerTorso.WaistRigAttachment
		o.AlignType = Enum.AlignType.PrimaryAxisLookAt
		char.LowerTorso.WaistRigAttachment.Axis = Vector3.new(0,0,-1)
		o.RigidityEnabled = true
		o.ReactionTorqueEnabled = true
		o.Responsiveness = 50

		task.spawn(function() -- Trying to solve player not looking toward the hook, causing them to fly about
			while Barrel:GetAttribute("Enabled") == true do
				wait()
				o.LookAtPosition = hook.Position
			end
		end)
		
		o.Name = "AlignLook"
	end



	if not char.LowerTorso:FindFirstChild"VectorForce" then
		f = Instance.new("VectorForce")
		f.Parent = char.LowerTorso
		f.RelativeTo = Enum.ActuatorRelativeTo.Attachment0
		f.Attachment0 = char.LowerTorso:WaitForChild("WaistRigAttachment")

		f.Force = Vector3.new(0,game.Workspace.Gravity*char.PrimaryPart.AssemblyMass*0.8,0)
	else end
end

I’ve reviewed other code which does exactly what I want it to do and integrated it into this function, however the problem doesn’t seem to solve itself. The hook mechanism works perfectly when no moving objects are involved.

The hooks following any moving objects is pretty integral to the gameplay, so any help at resolving this issue would be appreciated!

1 Like

Do you mean it does not during the firing mechanism? As in, between firing and landing at the target?


This reply is based on that assumption

I noticed you are tweening it, which might not be the best option, considering when you set the goal you can’t change it unless you use multiple tweens… I’d recommend lerping (linear interpolation) to calculate a midpoint for your hook CFrame at short intervals over the length you want it to happen for.

local target = --the target part you want to move
local length = 1 --replace with time length you want it to take
local multiplyFactor = 100 --increase this for a greater amount of calculations

local startFrame = target.CFrame
local targetFrame = CFrame.new(--[[target position]]) * CFrame.fromEulerAnglesXYZ(target.CFrame:ToEulerAnglesXYZ())

local stop = math.floor(length * 100)

for i = 1, stop, 1 do
    --get the alpha, representing the current position within the iteration
    local alpha = i / stop

    --lerp for a new CFrame
    local newFrame = startFrame:Lerp(targetFrame, alpha)

    --assign this new CFrame
    target.CFrame = newFrame

    --wait an appropriate amount of time
    task.wait(1 / multiplyFactor)
end

Please note that task.wait can only wait up to a certain amount of time (1/60th of a second, depends on your frame rate as well) which means that if you set the length too low there will come a point where calculation speed won’t change.

Hope this helps!

if ur using a raycast i guess you could move the hook to the hit position and weld it to the targeted part??

Sorry, must’ve been my wording of it. I don’t have any issues with the hook moving toward the position it’s meant to, if anything it’s just slightly lower than where the player is aiming which could be because of the raycasting…
I want it to stay “grappled” on to the target object if it moves.

That’s exactly what I’ve tried to achieve in the script, except a Weld instance moves the hook to the targeted objects origin, and the WeldConstraint seems to do absolutely nothing.

maybe you used Part0 and Part1 incorrectly
part0 is the part which part1 follows

Still having issues with this whole thing, I’ve tried to change what part0 and part1 are bound to, tried using Welds instead as well as other random tinkering to get it to work.

I’m going to try adding an attachment to the target, and move the attachment to the hooks WorldPosition and see if that resolves the issue I’m facing.

Did some investigating and it’s because the objects CFrame needed to be adjusted as opposed to it’s Position, using WeldConstraints was technically working this entire time, I just wasn’t moving the part correctly.

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