Rope swinging system problem

I made this rope swinging system but it’s a bit buggy.

I’ve factored in the camera’s cframe’s look vector and used that to determine the direction, which worked except for this:

I thought it was to do with the shorter sides of rectangles, but for some reason it doesn’t work on squares for one side too?

I have no idea why this is happening and it’s really odd behaviour, here’s my code:

	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Exclude
	params.FilterDescendantsInstances = {char}
	
	local origin = hrp.Position
	local direction = mouse.Hit.Position - origin
	
	local raycast = workspace:Raycast(origin, direction, params)
	
	if raycast then
		if not (raycast.Normal == Vector3.new(0,-1,0)) or deb then
			return
		end
		local rope = Instance.new("RopeConstraint")
		rope.Thickness = .2
		rope.Color = BrickColor.new("Pink")
		rope.Visible = true
		
		hrp.Anchored = true
		
		local attachment0, attachment1 = Instance.new("Attachment", hrp), Instance.new("Attachment", raycast.Instance)
		attachment0.WorldPosition = hrp.Position
		attachment1.WorldPosition = raycast.Position
		
		rope.Attachment0 = attachment0
		rope.Attachment1 = attachment1
		rope.Length = (attachment0.WorldPosition - attachment1.WorldPosition).Magnitude
		rope.Parent = workspace
		
		local Origin = raycast.Instance
		local Bob = hrp
		
		local Length = (Origin.Position - Bob.Position).Magnitude
		local theta = math.rad(45)
		local angVel = 0
		local function Compute()
			local XArc = Length * math.sin(theta)
			local YArc = Length * math.cos(theta)
			--local ZArc = Length * math.rad(theta)
			
			Bob.CFrame = Bob.CFrame:Lerp(CFrame.new(Vector3.new(XArc , YArc, 0) * (workspace.CurrentCamera.CFrame.LookVector * Vector3.new(-2, 0, -2) + Vector3.new(0,1,0)), Vector3.new()) + 
				Origin.CFrame.Position, 0.05)
			angVel = (angVel + (0.01*math.sin(theta)))
			theta = theta + angVel
		end
		deb = true

		conn = game:GetService("RunService").Heartbeat:Connect(Compute)
		task.wait(.8)
		hrp.Anchored = false
		deb = false
		if conn then
			conn:Disconnect()
		end
		rope:Destroy()

I took a pendulum swinging mechanism code of off a post and I’ve tried to make it work, and it works except for that one problem

Can somebody please help

This is probably the main part of the code you’ll need to look at

Bob.CFrame = Bob.CFrame:Lerp(CFrame.new(Vector3.new(XArc , YArc, 0) * (workspace.CurrentCamera.CFrame.LookVector * Vector3.new(-2, 0, -2) + Vector3.new(0,1,0)), Vector3.new()) + 
				Origin.CFrame.Position, 0.05)

Try this.

Bob.CFrame = Bob.CFrame:Lerp(workspace.CurrentCamera.CFrame*Vector3.new(XArc , YArc, 0), 0.05)
1 Like

Get this error and when I try fix it by turning vector.new into cframe.new, it doesn’t make it work properly

Unable to cast Vector3 to CoordinateFrame

Try converting into a CFrame using CFrame.new

1 Like

I did that and it didn’t give me the results I wanted, it made me swing sideways

If that is the only other problem then you can add an extra rotation if it swings sideways by multiplying by CFrame.Angles(0,math.deg(90,)0)

Looks to me like the same result

Bumping because I still need help with this

Have you tried changing the axis to the Z axis or the lookvector. If I were the one with the problem I would be trying every possible combination.

Vector3.new(0, YArc, XArc )
1 Like

I tried everything, the look vector was the closest one when I multiplied it by 30 in the X axis but it only worked one way it seemed.

Try this:

local Tool = script.Parent
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local deb = false
local conn

Tool.Activated:Connect(function()
	if deb then return end

	local player = Players:GetPlayerFromCharacter(Tool.Parent)
	if not player then return end

	local char = Tool.Parent
	local hrp = char:FindFirstChild("HumanoidRootPart")
	if not hrp then return end

	local mouse = player:GetMouse()
	if not mouse then return end

	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Exclude
	params.FilterDescendantsInstances = {char}

	local origin = hrp.Position
	local direction = (mouse.Hit.Position - origin).Unit * 100  -- limit to 100 studs

	local raycast = workspace:Raycast(origin, direction, params)

	if raycast then
		if not (raycast.Normal == Vector3.new(0, -1, 0)) or deb then
			return
		end

		local rope = Instance.new("RopeConstraint")
		rope.Thickness = 0.2
		rope.Color = BrickColor.new("Pink")
		rope.Visible = true

		hrp.Anchored = true

		local attachment0 = Instance.new("Attachment", hrp)
		local attachment1 = Instance.new("Attachment", raycast.Instance)

		attachment0.WorldPosition = hrp.Position
		attachment1.WorldPosition = raycast.Position

		rope.Attachment0 = attachment0
		rope.Attachment1 = attachment1
		rope.Length = (attachment0.WorldPosition - attachment1.WorldPosition).Magnitude
		rope.Parent = workspace

		local Origin = raycast.Instance
		local Bob = hrp
		local direction = Origin.Position - Bob.Position
		local Length = (Origin.Position - Bob.Position).Magnitude
		local theta = math.rad(90)
		local angVel = 0
		
		local camera = workspace.CurrentCamera
		local function Compute(dt)
			local XArc = Length * math.sin(theta)
			local YArc = Length * math.cos(theta)
			
			print(Vector3.new(0, YArc, XArc))
			local bobCFrame = CFrame.new(CFrame.lookAlong(Origin.Position, direction*Vector3.new(1,0,1))*Vector3.new(0, YArc, XArc))
			Bob.CFrame = Bob.CFrame:Lerp(
				bobCFrame, 1
			)

			angVel = angVel + (0.05)*dt
			theta = theta + angVel
		end

		deb = true
		conn = RunService.Heartbeat:Connect(Compute)
		task.wait(0.8)
		hrp.Anchored = false
		deb = false

		if conn then conn:Disconnect() end
		rope:Destroy()
	end
end)

Thank you my man fixed my problem!