How do I make a minimum range?

I’m not very good with math and I’ve tried but I can’t seem to figure it out.

I get a variable for the mouse position when the player clicks like this.

local mouseP = mouse.Hit.p

However I don’t want the player to be able to click super far away. So when the player clicks to far away, the mouseP variable will default to a value closer to the player.

local magnitude = (gun.Nozzle.Position - mouseP).Magnitude 
if magnitude > range then
     -- calculate the mouse position but closer to the player
end

I am not sure how to calculate this. Can anyone help me?

For a time range (projectile which disappears after some length of time) you can do:

task.wait(1)
projectile:Destroy()
debris:AddItem(projectile, 1)

For magnitude compare the original position of the projectile with the current position, if the stud count exceeds a certain number then remove the projectile.

The useful thing about the mouse object is that you can use CFrame to figure out the rotation and distance of the projectile

(Realised you want to the nozzle, just use that part’s CFrame instead of Mouse.Origin)

First calculate the distance between Origin and Hit, If its below your limit, just use it, otherwise, continue on
(Origin.p - Hit.p).Magnitude

Cast a new CFrame pointing at your mouse’s Hit with CFrame.new(mouse.Origin.p, Mouse.Hit.p), then add it’s LookVector multiplied by your limit

FinalPosition = PointCFrame.p + (PointCFrame.LookVector * limit)

2 Likes

Thank you! I’m still getting used to all this math and problem solving using vectors and stuff.