How to move forward with LookVector with Raycast.Distance

I’m trying to get the distance between the HumanoidRootPart and the surface of a part the raycast detects, then position the HumanoidRootPart 0.35studs away from the surface. The character is already facing directly towards the surface and is closer than 5 studs.

hrp is HmanoidRootPart

local raycastResult = workspace:Raycast(hrp.Position, hrp.CFrame.LookVector * 5, rParams)
if raycastResult then
	print(raycastResult.Distance)
	hrp.Position = hrp.CFrame.LookVector * (raycastResult.Distance - 0.35)
end

The second to last line is wrong. How you do it?

You probably want to use MoveTo with Humanoid.

1 Like

I forgot setting the position directly causes glitches :sweat_smile:. I replaced it with these

  1. hrp.CFrame = CFrame.new(hrp.CFrame.LookVector * (raycastResult.Distance - 0.35))
  2. char:PivotTo(CFrame.new(hrp.CFrame.LookVector * (raycastResult.Distance - 0.35)))

Both moved the character to the center of the map. The math is wrong.

What are you trying to do?
Like a grappling hook system?

This is the playtest of the code without the raycast part

Theres a gap unless the character is already up against the wall. I’m trying to prevent that.

This is the related code. The part not working is in comment to prevent errors.

local wallGrab = false
print('amogus')


local params = OverlapParams.new()
params.FilterDescendantsInstances = {char:GetChildren()}
params.FilterType = Enum.RaycastFilterType.Include


while true do
	task.wait(0.05)
	if hum:GetState() == Enum.HumanoidStateType.Jumping or hum:GetState() == Enum.HumanoidStateType.Freefall then
		for i,ledge in pairs(workspace.MapParts.Ledges:GetChildren()) do
			for i,v in pairs(workspace:GetPartBoundsInBox(ledge.CFrame, ledge.Size, params)) do
				if v == hrp then
					if wallGrab == false then
						wallGrab = true
						hum.AutoRotate = false
						local liv = Instance.new("LinearVelocity")
						liv.Name = "WallAttachLv"
						liv.MaxForce = math.huge
						liv.VectorVelocity = Vector3.new(0,0,0)
						liv.Attachment0 = hrp:FindFirstChild("Attachment")
						liv.Parent = hrp
						
						if plr.CurrentFruit.Value == 'control' then
							local charYOffset = -1.8
							local distanceFromLedge = -0.35
							
							local offsetPosition = Vector3.new(hrp.Position.X, 0, hrp.Position.Z) + Vector3.new(ledge.CFrame.LookVector.X, 0, ledge.CFrame.LookVector.Z).Unit * -distanceFromLedge
							local finalPosition = Vector3.new(offsetPosition.X, ledge.Position.Y + charYOffset, offsetPosition.Z)
							local lookAt = finalPosition - Vector3.new(ledge.CFrame.LookVector.X, 0, ledge.CFrame.LookVector.Z).Unit
							hrp.CFrame = CFrame.new(finalPosition, lookAt)
							
							
							local rParams = RaycastParams.new()
							rParams.FilterDescendantsInstances = {}
							rParams.FilterType = Enum.RaycastFilterType.Include
							
							for i,v in pairs(workspace.MapParts:GetChildren()) do
								if v.Name == 'Baseplate' then
									rParams:AddToFilter(v)
								end
							end
							
							--[[
							local raycastResult = workspace:Raycast(hrp.Position, hrp.CFrame.LookVector * 5, rParams)
							if raycastResult then
								print(raycastResult.Distance)
								hrp.CFrame = CFrame.new(hrp.CFrame.LookVector * (raycastResult.Distance - 0.35))
								char:PivotTo(CFrame.new(hrp.CFrame.LookVector * (raycastResult.Distance - 0.35)))
							end ]]
							
							LawAttachAT:Play()
						
						
						elseif plr.CurrentFruit.Value == 'magnet' then
							KidAttachAT:Play()
						end
					end
				end
			end
		end
	end
end

Nice

Anyway, the way that wallgrabs (no idea if the term is right) works, is by glueing the character to the wall. I recommend using WeldConstraint for this part.
Then, if the player wants to move to the left, you need to see which direction the humanoid is pointing to, then move to the -RightVector or RightVector of the CFrame.

I don’t recommend using Physics to keep the player up “floating”

LinearVelocity with 0 velocity is working really good with mid air attacks done in random positions tho. y u dont recomment it

The overlap params area is too small so I want to make it bigger, but then the gap will be really noticeable. After the wall attach the character can jump away or quickly climb up it & attack. I need to fix this before making that.

I’m not sure I understood, but the solution to your original question would be:

hrp.Position = hrp.Position + hrp.CFrame.LookVector * (raycastResult.Distance - 0.35)

In practice, the surface may not be perfectly perpendicular, so alternatively you could do:

hrp.Position = raycastResult.Position + raycastResult.Normal * 0.35

You are setting the position of the HumanoidRootPart by using LookVector, but LookVector only represents the direction the CFrame is looking; not a real position. (LookVector axis will always be between -1 and 1)
Try this instead:

local humanoidRootPartPosition = hrp.Position
	local lookVector = hrp.CFrame.LookVector
	local distance = raycastResult.Distance
	local offset = lookVector * .35
	local newPosition = humanoidRootPartPosition + ((lookVector * distance) - offset)
	hrp.CFrame = CFrame.lookAlong(newPosition, lookVector)
1 Like

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