Raycast to 3D Position

I’m trying to raycast from mouse position to a valid 3d position the player can travers to, however most of the time the player goes in the air or just doesn’t do anything, any better approach?

local _r = rayResult(UserInputService:GetMouseLocation().X, UserInputService:GetMouseLocation().Y)

if _r then
	Player.Character:FindFirstChildOfClass("Humanoid").Jump = true
	Tool.RemoteEvent:FireServer(_r.Position)
end
local _thrustVelocity = Instance.new("BodyPosition")
_thrustVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
_thrustVelocity.P = 40000
_thrustVelocity.Position = Pos
_thrustVelocity.Parent = Character.HumanoidRootPart
2 Likes

What defines a valid position for the player to travel to? Is it an issue with the raycasting (print position to check) or an issue with the BodyPosition?

1 Like

It is related to that it sometimes it’s in the air no matter what

1 Like

It’s more so supposed to look like this https://www.youtube.com/watch?v=lO4mY05kuOQ 0:20 but with physics it’s a difficult approach

I tried to use @EgoMoose formula however it doesnt seem to translate well in accordance with the mouse position.

local t = 1;
local mouse = game.Players.LocalPlayer:GetMouse();
local hrp = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart");

mouse.Button1Down:Connect(function()
	local g = Vector3.new(0, -game.Workspace.Gravity, 0);
	local x0 = hrp.CFrame * Vector3.new(0, 2, -2)

	-- calculate the v0 needed to reach mouse.Hit.p
	local v0 = (mouse.Hit.p - x0 - 0.5*g*t*t)/t;

	-- have the ball travel that path
	local bv = Instance.new("BodyVelocity")
	bv.Velocity = v0
	bv.Parent = hrp
	
	local nt = 0;
	while (nt < t*2) do
		bv.Velocity = CFrame.new(0.5*g*nt*nt + v0*nt + x0).Position
		nt = nt + game:GetService("RunService").RenderStepped:Wait();
	end
	print("reached")
	bv:Destroy()
end)

t