I need help with part(hitbox) moving

I am making a hitbox appear out of the humanoidrootpart but I don’t know how to make it so that it moves out of the body and in front of the character. If I change x by one it can either go forward, sideways, diagonally, or backward based on the orientation. I need it to appear in front of the body.

I can come up with 2 different ideas - what one you use will depend on your situation/needs.

Option 1) You could use weldconstraints to weld the hitbox to the humanoidRootPart. You can adjust your weld so that the hitbox is in front of the root part.

Option 2) You could use runservice to constantly update the position of your hitbox in front of your root part using a vector3 as an offset. Probably with something like this:
hitbox.position = humanoidRootPart.CFrame:PointToWorldSpace(offset)

how can you adjust the weld?
this is my script:

local hitbox = Instance.new("Part")
				local hR = player.Character.HumanoidRootPart
				local hitbox = Instance.new("Part")
				local x = hR.Position.X 
				local y = hR.Position.Y - 0.5
				local z = hR.Position.Z 
				local x2 = hR.Orientation.X
				local y2 = hR.Orientation.Y - 90
				local z2 = hR.Orientation.Z
				hitbox.Position = Vector3.new(x, y, z)
				hitbox.Orientation = Vector3.new(x2, y2, z2)
				hitbox.Size = Vector3.new(5, 2, 3)
				hitbox.CanCollide = false
				hitbox.Transparency = 0.8
				hitbox.Color = Color3.new(1, 0, 0)
				hitbox.Parent = player.Character
				hitbox.Name = "hitbox"
				local weld = Instance.new("WeldConstraint")
				weld.Parent = player.Character.HumanoidRootPart
				weld.Part0 = hitbox
				weld.Part1 = player.Character.HumanoidRootPart

My mistake, you will want to use normal welds, not weld constraints to do this.

local weld = Instance.new("Weld")
weld.Parent = player.Character.HumanoidRootPart
weld.Part0 = hitbox
weld.Part1 = player.Character.HumanoidRootPart
weld.C1 = CFrame.new(0,0,2) -- This is the offset to adjust the weld

I think you’re looking for something like this.
hitbox.CFrame = hR.CFrame + hR.CFrame.LookVector.Unit * Distance

This would center the hitbox Distance studs in front of the HumanoidRootPart. You could remove many lines with this, and if you need to change the orientation or position, you could multiply or add (one or the other, I when to use which) Vector3s to the CFrame

hR.CFrame.LookVector.Unit will get the unit, foward facing vector of the hR CFrame. There are other options besides LookVector as well. See the documentation for more information.

CFrame | Documentation - Roblox Creator Hub

thank you very much kind maam :happy1:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.