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