Intersection point of raycast not where expected

I’m currently creating a wall climbing system. I’m finding the walls by raycasting, and if they are close enough to the wall then they will be attached to the wall. However, when I create a part at the raycast intersection(raycastResult.Position), it is in some random spot on the wall, not on the point where I’m looking. Not only that, but raycastResult.Distance is way larger than it should be(showing 20 when I’m like 5 studs away from the wall)

My code:

	local hum = char.Humanoid
	local hrp = char.HumanoidRootPart
	
	local playerIsClimbing = false
	
	while wait(1) do
		local wall = workspace.Wall
		if not wall then continue end
		if not hrp then continue end
		
		local raycastParams = RaycastParams.new()
		raycastParams.FilterType = Enum.RaycastFilterType.Exclude
		raycastParams.FilterDescendantsInstances = {char}
		
		local direction = (wall.Position - hrp.Position).Unit*150
		local raycastResult = workspace:Raycast(hrp.Position, direction, raycastParams)
		
		if not raycastResult then continue end
		
		if raycastResult.Instance then
			if raycastResult.Instance == wall then
				local part = Instance.new("Part")
				part.Position = raycastResult.Position
				part.Anchored = true
				part.CanCollide = false
				part.BrickColor = BrickColor.random()
				part.Parent = workspace
				
				if raycastResult.Distance <= 4 and not playerIsClimbing then
					
					local alignPosition = Instance.new('AlignPosition')
					alignPosition.Parent = hrp
					
					local alignOrientation = Instance.new('AlignOrientation')
					alignOrientation.Parent = hrp
					
					local attachment0 = Instance.new("Attachment")
					attachment0.Name = "Attachment0"
					attachment0.Parent = hrp
					attachment0.Position = Vector3.new(0,0,0)
					
					local attachment1 = Instance.new('Attachment')
					attachment1.Name = "Attachment1"
					attachment1.Parent = wall
					attachment1.WorldPosition = raycastResult.Position
					
					
					local attachment0Orientation = Instance.new("Attachment")
					attachment0Orientation.Name = "Attachment0"
					attachment0Orientation.Parent = hrp
					attachment0Orientation.Position = Vector3.new(0,0,0)
					
					local attachment1Orientation = Instance.new("Attachment")
					attachment1Orientation.Name = "Attachment1"
					attachment1Orientation.Parent = wall
					attachment1Orientation.WorldPosition = raycastResult.Position
					
					alignPosition.Attachment0 = attachment0
					alignPosition.Attachment1 = attachment1
					alignOrientation.Attachment0 = attachment0Orientation
					alignOrientation.Attachment1 = attachment1Orientation
					
					hum.PlatformStand = true
					hrp.CFrame = CFrame.new(raycastResult.Position, raycastResult.Normal)
					
					playerIsClimbing = true
				end
			end
		end
	end

Hi bleintant,
At a glance, it looks like you’re computing the direction by using the wall’s position, which will always be the center of that part (assuming the pivot was not modified). Perhaps you were looking for HumanoidRootPart.CFrame.LookVector **as a factor in the equation?

I didn’t know that it would always get the center. Where would I put “HumanoidRootPart.CFrame.LookVector” in my code? Would it be as part of the direction?

Nevermind, figured it out.

–Characters

1 Like

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