Calculating position based on mouse.Hit.position

Ok so how do I calculate the position to where the player is looking with a magnitude of 50?
Here is a video of my problem it doesn’t move if the mouse.Hit.Position is far because of a restriction of 50 magnitude but I still want to move it in that direction its facing with a magnitude of 50
https://gyazo.com/be67c4015698a95dd7e764b7884952d9
Here is the code I used

local newPart2 = Instance.new("Part")
			newPart2.Anchored = true
			newPart2.CanCollide = false
			newPart2.Position = Vector3.new(0,0,0)
			newPart2.Parent = workspace
			newPart2.Transparency = .65
			newPart2.Material = Enum.Material.Neon
			newPart2.Size = Vector3.new(15,2,15)
			swordStormCon = runService.RenderStepped:Connect(function()
				mouse.TargetFilter = newPart2
				local directionHit = mouse.Hit.Position
				print(directionHit)
				if (rootPart.Position - directionHit).Magnitude <= 50 then
					newPart2.Position = directionHit
				end
			end)

Without putting a lot of thought into what the exact mathematics would be, I would imagine that you could add a simple else statement onto the magnitude check that then calculates what position would be 50 studs out and then assigns it to that.

Alternatively, you could also look into using raycasting as another method to achieve the same goal. See this page (Raycasting | Documentation - Roblox Creator Hub) for more on that.

So you mean like character.HumanoidRootPart.CFrame.lookVector * 50?
but I dont think that’s gonna give me a magnitude of 50 I think
And with raycasting uhh I dont know what to say

You’re on the right track, though it depends on how you want to handle it exactly. I would suggest you look at the vector between the HumanoidRootPart and mouse.Hit (don’t use the lookVector, since you want to base it off of mouse position) and just follow that 50 studs out.

Hmm I see but its based on mouse.Hit.Position which means if i based it on mouse pos its not what exactly what I want since I want the position to be sticking to a surface.

All you have to do is clamp the positions. Here’s what I wrote;

local x, y = math.clamp(directionHit.X, rootPart.Position.X-range, rootPart.Position.X+range), directionHit.Y
local z = math.clamp(directionHit.Z, rootPart.Position.Z-range, rootPart.Position.Z+range)
newPart2.Position = Vector3.new(x,y,z)

note that the variable ‘range’ is 50, which is the max distance you want the part to go out

1 Like

I’m talking about mouse position generally here as a reference to mouse.Hit.Position. Given that the position is provided in vector format for both mouse.Hit.Position and for HumanoidRootPart.Position, you should be able to find the exact location 50 studs along that connecting vector.

Unless you mean to clarify that any parts between the HumanoidRootPart and mouse.Hit.Position also need to be taken into account, in which case I highly advise spending some time learning to raycast.

1 Like

I still have a question how would i check in server if that vector3 has a range of 50
because you know exploiters can easily change it

Do what you did before, check the magnitude between the root part and the chosen position, and go from there. If it’s over 50 then it was exploited

1 Like

There’s a little bit of a hiccup the y pos is not clamp which means if i look up the y pos goes off to eternity.

add this at the beginning of the render stepped function

if not mouse.Target then return end
1 Like

Im dumb really really f dumb didnt think of that

You’re all good :slight_smile: