Restricting how far something can move?

Hello! The title might be a bit confusing so let me explain.

I’m trying to make a ball shoot towards where your mouse is pointing, however using Mouse.Hit.p makes it go wherever I put my mouse, so if I would point my mouse up at the sky it just flies away. Is there a way to make it go towards where I’m pointing my mouse but only a bit, say 50 studs.

I tried Mouse.Hit.LookVector but without luck.

1 Like

Basically, you would want to do
(Gun.Position-Mouse.Position).Magnitude * 50

Keep in mind that I do not know your exact case, so you would have to improvise a bit on your own to make that work

3 Likes

Thanks a lot! I’ll try it when I can :blush:

1 Like

I think you’re supposed to use Unit, since both are Vector3 types and I don’t think you can get the direction using Magnitude. Also, Mouse.Position should go first before Gun.Position, or else you get the inverted direction

1 Like

Shouldn’t the gun position be first since that’s where the ball is going to shot from? Also, I’ve used magnitude for the past 3 years and it has never betrayed me, there might be better methods out there, but magnitude works greatly for me.

1 Like

Incorrect, I’ve already tested this. It also makes more sense that you’re inverting it cause we’re talking subtraction

1 Like

By the way, i was incorrect about magnitude - magnitude is simply length, so use .Unit * 50

I tried this, didn’t work. I’ll show you what my code looks like,

local tween = TweenService:Create(tornado, TweenInfo.new(2), {Position = (mouse - humrp.CFrame.p).Unit * 50})

the “mouse” variable is Mouse.Hit.p sent from a localscript.

1 Like

just do HumanoidRootPart.Position instead of CFrame.P, and instead of mouse.CFrame.P, do mouse.CFrame.Position, I believe that this could fix it


https://gyazo.com/74c48945b98be0b5361aaaab07de44a5

1 Like

Sorry, i meant Mouse.Hit.Position, not CFrame

Alright I’ll send you a video so that you can see what’s going on. https://streamable.com/uiyaas

local tween = TweenService:Create(tornado, TweenInfo.new(2), {Position = (mouse - humrp.Position).Unit * 50})
tween:Play()

And the mouse is,
remote_Q:FireServer(mouse.Hit.Position)

Streamable is blocked in my country.

You need to add the vector to the humanoidrootpart’s position because that vector is only going to be 50 studs away from the origin, facing the mouse’s position in the world. Also, this isn’t really limiting it, as this code will always just make the ball go 50 studs in the direction of your mouse from the humanoidrootpart, which is unintended. You can try this:

local vec = mouse - humrp.Position
local target = humrp.Position + vec.Unit * math.min(vec.Magnitude, 50)

local tween = TweenService:Create(tornado, TweenInfo.new(2), {Position = target})
tween:Play()
1 Like