I need to make a hitbox gradually expanding to the end of the fire with the help of raycasts, limited to this fire. What ideas do you have about this?
It does not need to be gradually expanding, its eaiser to keep the rays the same length then detect where parts intersect with the ray. This functionalyity is included in the roblox raycast object… If the poitn of intersection is less than the current length of the flame, then do stuff. or dont…
You’re right, but I mean, how can you accurately calculate the particle area for a hitbox?
I think the answer is just rough it.
Here the green lines represent the raycasts and the killbox created is represented by the cone made of the green lines and the red box. You want the particles to roughly cover the same ares as the cone.
Quite good! I think it gives a decent idea of that. However, could you provide an approximate way to implement this, that is, how to make the cone-shaped hitbox shown in your illustration?
local Workspace = game:GetService("Workspace")
local function castRay()
-- The origin point of the ray
local originPosition = Vector3.new(<the end of the gun>)
-- The direction the ray is cast in
local direction = Vector3.new(<the direction the gun is facing>)
-- The maximum distance of the ray
local distance = 50
-- Cast the ray
local raycastResult = Workspace:Raycast(originPosition, direction * distance)
--if ray hits something then
if raycastResult then
-- Print all properties of the RaycastResult if it exists
print(`Ray intersected with: {raycastResult.Instance:GetFullName()}`)
print(`Intersection position: {raycastResult.Position}`)
print(`Distance between ray origin and result: {raycastResult.Distance}`)
print(`The normal vector of the intersected face: {raycastResult.Normal}`)
print(`Material hit: {raycastResult.Material.Name}`)
else
print("Nothing was hit")
end
end
-- Continually cast a ray every 0.1 seconds
while true do
castRay()
task.wait(0.1)
end
This will fire one ray directly forward, if you want more rays just repeat and if you want a cone just offset the rays direction a bit for each one - this will be dependant on the current rotation and direction of the weapons cFRame
Yes, that’s exactly how I want to implement it. But I need to find out how I can draw a pyramid using a script to solve this problem?
you draw the pyramid with raycasts…the angle of the slope is drawn by offsetting raycast directions from the point, and the bas is made by setting the distance of the raycasts…
tbh you could just forget about the whole raycast thing and just attach an invisible part to the front of the gun and then do all the damage logic with the .touched event
Do not use .Touched() for anything but objects that are stationary and may not be activated for long periods of time.
Anyways, I thought of a different way to do this. Basically, you use :GetPartsInBox() infront of whatever you want to shoot fire, and you then filter parts based on how far they are away they are from the center of the hitbox and how far they are away from the origin.
This is probably better explained with a diagram.
The black box is the origin, and the red line is the lookvector of the black part. The green box is the our spatial query, which will bring us all the parts it intersects. Finally, to create a cone-like shape, the blue lines represent the acceptable distance from the center line in comparison to the distance from the origin.
It’s a good implementation, but I have a question. How to determine after we have received objects in the area whether they are in the pyramid released from the origin?
Ok the worlds eaisest way that requires no maths.
have a gun with 3 hitboxes attached to the tip, that roughly repesent the cone of the paticles.
Then use :GetPartsInBox() on each of them every frame.
so
local hitboxes {hitbox1,hitbox2,hitbox3}
game:GetService("RunService").Stepped:Connect(function()
for i,v in ipairs(hitboxes)do
if v:GetPArtsInBox() then --i havent checked how this actually works... i assume it doesnt just return true
--kill or whatever
end
end
)
Yes, this is the easiest way. However, I want to determine the cone exactly, and not with the help of three hitboxes.
I’m tired but I’ll throw out a meme idea that might work but def not the best or performant. Anyway, track how long the flames go from start to finish then tween a part’s size. This part will gradually get bigger and you can just loop the partsinbound function to detect anything hit by the fire
You’d need to get the offset of the hit part from the origin, which I think can be done with CFrame:ToObjectSpace(CFrame). However you do it, you get a vector3.
The Z component is distance, and we can find how far away something is from the center by converting the X and Y components to a vector2 and using magnitude.
local Offset = HitPart.CFrame:ToObjectSpace(Origin.CFrame) --//Might not be the right method.
local Length = Offset.Position.Z
local Distance = Vector2.new(Offset.Position.X,Offset.Position.Y).Magnitude
Having checked many times, I think that this method is still not ideal.