Set a max position where hitbox to be placed

I am trying to figure out a way to place a hitbox where my mouse would be placed. I already got the part of the script where if the magnitude between the HumanoidRootPart and Mouse positions meet lower than the area, then it will place the hitbox. I also want a way to get the hitbox to get as close as possible towards the mouse, but still regarding the restraint. Here is some of my code.

local hitbox = game.ReplicatedStorage.Objects.Galactic.GalacticShowerHitbox:Clone()
	hitbox.Parent = workspace
	
	local area = 40
	local dist = (pos.Position - hrp.Position).Magnitude
	
	if locked then
		hitbox.CFrame = hrp.CFrame
	elseif not locked then
		if dist <= area then
			hitbox.Position = pos.Position
		else
			local dir = (pos.p - hrp.Position).Unit
			hitbox.Position = Vector3.new(40, 40, 40) * dir
		end
	end

Thanks in advance! :slight_smile:

10 Likes

So you want to the hitbox being placed the nearest possible of the mouse position in relation of the hrp?

I would like the hitbox to be still following the mouse position, but still inside the area/boundaries.

So you mean like you want to put the hitbox on the mouse.position but still obeying the restriction? I can’t understand what you desire to achieve, do you want to make the hitbox on the orientation of the mouse, position of the mouse?

If the case is the Position of the mouse, then you need to make some math though!!

I’m trying to make it to where if the boundary is met, then it will place the hitbox regardless, but if the mouse if past the boundary, it will still place the hitbox, but it won’t be exactly on top the mouse position, just around the area I want it to be at. While also being in line between the root part and mouse positions

I got it! Ima try to help with that.

Are you trying to get the X position, Y position?

The Y position is fine, but the X and Z values are what are suppose to change

1 Like
local hitbox = game.ReplicatedStorage.Objects.Galactic.GalacticShowerHitbox:Clone()
	hitbox.Parent = workspace
	
	local area = 40
	local dist = (pos.Position - hrp.Position).Magnitude
	
	if locked then
		hitbox.CFrame = hrp.CFrame
	elseif not locked then
		if dist <= area then
			hitbox.Position = pos.Position
		else
			local dir = (pos.p - hrp.Position).Unit
			hitbox.Position = Vector3.new(40, 40, 40) + dir -- Vector3.new(40,40,40) (the limit) + dir which is the .Magnitude of 1.
		end
	end

Have you ever trying to add a β€œ+” instead of a β€œ*”?

Thank you for the help, I have to see if it works in the morning

1 Like

Sure! Remember Vector3 cannot be used a β€œ*” to increase its value though!

1 Like

I tried the code you provided, but the hitbox keeps going all the way off the map.

Someone? I still need help on my topic :frowning:

Can you show a video or something?

You should learn more about Math functions here math | Documentation - Roblox Creator Hub

Have you ever tried something like this?