How do I create a part on the size and position and angle of a raycast weather it hit something or it did not

Hi!, So I hope I posted this on the right category, So I have recently been trying to make a combat system and it is raycast based like on minecraft, Basiclly it doesnt matters if your weapon model hits your opponent because its not collision based, But if your oponent is in range and in your cross hair then he takes damage, So I recently decided to make this functional but I ran into an issue and its that it is extremely hard to hit someone because the hitbox is a single ray that gets casted to the position of the mouse, So my solution is creating a part on the exact position of the ray and angle and length (And obviously stretching it a bit from all of its sides so it exapands the hitbox) and then using :GetPartsInPart on that part created to get the humanoids on it and deal dammage or just a touched event and that way deal dammage, But heres the issue I have no idea how to create that part because as far as I know when a raycast does not gets a result (does not hits an object) it doesnt gives any data that could be used, So how would create a part with the things i mentioned without needing the result.

Yes and I did try to research this before making this post, but most posts were unclear and did not explained anything, So I am making this post as a last resource.

Heres my code:

local ListRayCastParams = RaycastParams.new()

	ListRayCastParams.FilterType = Enum.RaycastFilterType.Blacklist
	
	ListRayCastParams.FilterDescendantsInstances = {MyCharacter, FireAxeTool.FireAxeModel}
	
	-- Get Factors to create raycast
	local RayCastOrigin = MyCharHead.Position

	local RayCastDirection = (MousePosition - RayCastOrigin).Unit

	local RayCastResult = workspace:Raycast(RayCastOrigin, RayCastDirection*6, ListRayCastParams)
	
	-- Hitbox Creator --
	
	local CastedHitbox = Instance.new("Part", game.Workspace)
	
	CastedHitbox.Name = "CASTEDHITBOX"
	
	CastedHitbox.CanCollide = false
	
	CastedHitbox.Anchored = true

	end

end

If I was unclear or missed any critical info please reply and I will clarify

1 Like

instead of using raycast you can just use Mouse | Roblox Creator Documentation

something like this,

local sword = -- your sword
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

sword.Activated:Connect(function()
    local target = mouse.Target
    if target ~= nil then
        if target.Parent:FindFirstChild("Humanoid") then
            -- fire remote event here and server will handle the damage function
        end
    end
end)

although you can still use raycast if you prefer it more

1 Like

I really reallly do need to use raycast on this scenario so yeah your solution still does not helps because my original purpose was to expand the hitbox and even if i used this it brings the same problem of being hard to hit a player bc of the tiny hitbox, and also that method is client sided in some way to detect hits wich kinda makes my game insecure.

And also I wanna learn this to make lasers come out of guns that i will code wich if i were to do your solution it wouldnt help me fully learn what i asked to.

Oh yeah I forgot to mention that i want hitboxes to work on the server, Im fully aware that to get the mouse position you need a local script and firing a remote with that position to the server to make that.

oh, if that’s what you want to do then maybe this will help. Raycast Hitbox 4.01: For all your melee needs!

No you arent getting my point, That is a collision based hitbox because the hit is registered by your weapon hitting the enemy, I specifiied i didnt wanted a collision based engine and I wanted something like Minecraft for example, Where hits are registered by firing a raycast from your camera or head into your mouse direction, I have that system but now i wanna create a part on the raycast with its length and position and direction

oh okay, i get it now. do you mean something like this? How would I cast a ray to where my mouse hit, and then you’re creating a part with the length of the raycast with the same position as it? (like making the raycast visible?)

Yes exactly like that!, Thats where I’m stuck tho.

oh okay, well maybe this would help How do you visualize a raycast as a part?

Again as I said, I researched it and yes I found that but theres no explanation to what they are doing and leaves me confused, But I guess Il’l give it another read and see if I can understand it now.

oh okay, well. wait.

asdassdasdasd

can you try this script?

ListRayCastParams.FilterType = Enum.RaycastFilterType.Blacklist
	
	ListRayCastParams.FilterDescendantsInstances = {MyCharacter, FireAxeTool.FireAxeModel}
	
	-- Get Factors to create raycast
	local RayCastOrigin = MyCharHead.Position

	local RayCastDirection = (MousePosition - RayCastOrigin).Unit

	local RayCastResult = workspace:Raycast(RayCastOrigin, RayCastDirection*6, ListRayCastParams)
	
	-- Hitbox Creator --

if RayCastResult then
    local position = RayCastResult.Position
    local distance = (origin - result.Position).Magnitude
    local p = Instance.new("Part")
    p.Anchored = true
    p.CanCollide = false
    p.Size = Vector3.new(0.1, 0.1, distance)
    p.CFrame = CFrame.lookAt(RayCastOrigin, position)*CFrame.new(0, 0, -distance/2)
end

What about using Mouse.UnitRay and making the ray visible by calculating the midpoint between bullet’s starting position and Ray’s hit position or default set length? Ex:

local Players = game:GetService("Players")
local RS = game:GetService("RunService")

local Client = Players.LocalPlayer or Players:GetPropertyChangedSignal("LocalPlayer"):Wait()
local Mouse: Mouse = Client:GetMouse()

local BulletStartPos = script.Parent.StartPos

local function MakeRayVisible(pos1, pos2)
	local Distance = (pos1-pos2).Magnitude
	local MidPoint = (pos1+pos2)/2
	
	local NewPart = Instance.new("Part")
	NewPart.Anchored = true
	NewPart.CanCollide = false
	NewPart.Material = Enum.Material.Neon
	NewPart.Size = Vector3.new(.1, .1, Distance)
	NewPart.CFrame = CFrame.lookAt(MidPoint, pos2)
	NewPart.Parent = workspace
	
	return NewPart
end

RS.RenderStepped:Connect(function()
	local Origin, Direction = BulletStartPos.Position, Mouse.UnitRay.Direction*300
	local Result = workspace:Raycast(Origin, Direction)
	
	local Intersection = Result and Result.Position or Origin + Direction
	local VisibleRay = MakeRayVisible(Origin, Intersection)
end)

If I’m understanding this correctly it seems like you want to do all the hit detection through the part itself, and were just using the ray for obtaining the initial direction? If that’s the case there isn’t really any need for the ray.

You can try getting the direction using just the mouse position and player’s head, and then place a part of whatever desired hitbox size you want in front of the player.

local hitboxLength = 6
local hitboxWidth = 0.5
	
local hit = Mouse.Hit.p
local dir = (hit - Player.Character.Head.CFrame.p).unit
	
local part = Instance.new("Part")
part.Anchored = true
part.Size = Vector3.new(hitboxWidth, hitboxWidth, hitboxLength)
part.CFrame = CFrame.new(Player.Character.Head.CFrame.p + (hitboxLength / 2 * dir), hit)
part.Parent = workspace
-- handle whatever checks you need and clean up

Uhhh could you explain what it does on the Cframe and size part im kinda confused…

So i kinda need it to directly do what I said, Since yes I could do your method but I kinda want also to learn how to make a part on the raycast so i can make some effects for guns like lasers with parts and not with beams or particle emiters.

I wish I was such an advanced coder to understand this, Could you explain please what it does, sorry if this comes as an anoyance, I am legit struggling to understand most of this script.

the part gets positioned to the middle of the raycast, can you try it?

Oh I see but theres just one problem I need the result of the raycast but what if i dont hit anything.

it checks if it’s a true or nil in here

function a:BulletHole(Position,BulletSize,Normal) -- Position is RaycastResult.Position, Normal is RaycastResult.Normal
	local p = Instance.new("Part")
	p.CFrame = CFrame.new(Position, Position + Normal)
	p.Size = Vector3.new(BulletSize,BulletSize,0.1)
	p.Transparency = 1
	p.Anchored = true
	p.CanCollide = false
	p.Position = Position or assert(Position == nil,"No position passed in BulletHole method for Object " .. self.WeaponModel.Name)
	p.Parent = workspace.BulletHoles
	local d = Instance.new("Decal")
	d.Texture = "rbxassetid://4784881970"
	d.Face = "Front"
	d.Parent = p
	coroutine.wrap(function()
		wait(20) -- Disappear after 3 seconds
		p:Destroy()
	end)()
end