Dash distance limit by using raycast

Hello. I really suck at Vector3/CFrame calculations and I have been trying to do this for some time but I am doing something wrong that I can’t detect. I am basically trying to limit the distance of the player’s dash if the raycast detects that there is an object nearby, because otherwise the character gets flinged. Here is what I have done so far:

			local direction = CFrame.new(0,0,-dashSpeed)
			local dashType = "Front"
			local dashLength = 0.3

			if UIS:IsKeyDown(Enum.KeyCode.A) then
				direction = CFrame.new(-dashSpeed,0,0)
				dashType = "Left"
				dashLength = 0.3
			end

			if UIS:IsKeyDown(Enum.KeyCode.D) then
				direction = CFrame.new(dashSpeed,0,0)
				dashType = "Right"
				dashLength = 0.3
			end

			if UIS:IsKeyDown(Enum.KeyCode.S) then
				direction = CFrame.new(0,0,dashSpeed)
				dashType = "Back"
				dashLength = 0.3
			end

			local vel = Instance.new("BodyPosition")
			vel.Parent = character.HumanoidRootPart
			vel.MaxForce = Vector3.new(9999999, 0, 9999999)
			vel.D = 2200
			vel.P = 80000
			
			local raycastParams  = RaycastParams.new()
			raycastParams.FilterType = Enum.RaycastFilterType.Exclude
			raycastParams.FilterDescendantsInstances = {character,workspace.debris}
			
			local raycastResult = workspace:Raycast(hrp.Position, (character.HumanoidRootPart.CFrame * direction.Position), raycastParams)
			if raycastResult then
				local newDirection = direction - raycastResult
				vel.Position = character.HumanoidRootPart.CFrame * newDirection.Position
				print("raycast")
			else
				vel.Position = character.HumanoidRootPart.CFrame * direction.Position
				print("regular")
			end

Regular dashes are fine, I just can’t get the raycast one to work. It doesn’t print, so I don’t think I am casting it to right direction. Thanks in advance.

1 Like

You’re casting from the HumanoidRootPart’s position along direction’s position, which is casting probably nowhere near where you want it to. It would probably better to cast along the HumanoidRootPart’s LookVector from it’s position.

local raycastResult = workspace:Raycast(hrp.Position, (hrp.CFrame.LookVector * 15), raycastParams)

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