How can I get accurate position results using Raycasting while being moved by a BodyVelocity?

Hi DevForum,

The title is self explanatory, I just need a way to get accurate position results when raycasting below the character, while they’re being moved by a bodyvelocity in an arc-like path.

Here’s the code:

RS.RemoteEvent.OnServerEvent:Connect(function(player, data)
	local character = data.Character
	
	if character then
		local hrp = character.HumanoidRootPart
		
		local yinitialSpeed = 85

		local ySpeed = yinitialSpeed
		local xSpeed = 65
		local gravityRate = 10
		local gravityExponentIncrease = 3
		local intervalTime = 0.125

		local bodyVelocity = Instance.new("BodyVelocity")
		bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
		bodyVelocity.Parent = hrp
		
		local connection

		connection = RunService.Heartbeat:Connect(function(dT)
			local rayOrigin = hrp.Position
			local rayDirection = rayOrigin - Vector3.new(0, 55, 0)

			local raycastParams = RaycastParams.new()
			raycastParams.FilterType = Enum.RaycastFilterType.Include
			raycastParams.FilterDescendantsInstances = {workspace.Map}
			
			local xVelocity = hrp.CFrame.LookVector * xSpeed
			local yVelocity = hrp.CFrame.UpVector * ySpeed

			bodyVelocity.Velocity = (xVelocity + yVelocity)

			if ySpeed <= 0 then
				gravityRate += gravityExponentIncrease * (dT / intervalTime)
			end

			ySpeed -= gravityRate * (dT / intervalTime)
		end)
	end
end)
1 Like

You’re not raycasting in the heartbeat function but if you do, rayDirection should just be:

local rayDirecton = Vector3.new(0, -1, 0) * raycastDistance
1 Like