So I’m working on a fireball attack and the raycasting so far is working pretty smoothly. Theres only one issue: If you aim at a player and said player moves before the fireball hits them, it explodes anyway. I want the fireball to continue if the player isn’t there when it reaches them, and i also want to make it so that if a player runs into the fireball it will explode.
script:
MouseReply.OnServerEvent:Once(function(returningPlayer, mousePosition)
if returningPlayer ~= myPlayer then
return
end
local origin = myCharacter["Right Arm"].Position
local direction = (mousePosition - origin).Unit * 100
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {
game.Workspace.TestPart,
ProjectileClone,
myCharacter["Right Arm"],
myCharacter["Left Arm"],
myCharacter["Right Leg"],
myCharacter["Left Leg"],
myCharacter.Head,
myCharacter.HumanoidRootPart
}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.IgnoreWater = true
local raycastResult = workspace:Raycast(origin, direction, raycastParams)
local hitPos = raycastResult and raycastResult.Position or (origin + direction)
local distance = (hitPos - origin).Magnitude
local speed = 100
local duration = distance / speed
local tween = game:GetService("TweenService"):Create(
ProjectileClone,
TweenInfo.new(duration, Enum.EasingStyle.Linear, Enum.EasingDirection.Out),
{Position = hitPos}
)
tween:Play()
tween.Completed:Once(function()
ProjectileClone.Transparency = 1
ProjectileClone.Explosion:Emit()
end)
if raycastResult then
if raycastResult.Instance then
print("Hit:", raycastResult.Instance.Name)
end
end
end)
I think you should try and use a hitbox rather than using a raycast i heard raycast can be costly in some scenarios not sure. I believe you can use :GetPartsBoundInBox() and then check if they are in the box (hitbox) if they are you can play the explosion. You can weld the hitbox to the fireball and unanchor it, this way it follows with the tween.
I am unable to write a script right now since i am not home, but when i do which would probably be tomorrow i can do a little mock up. For now could you try to refer to this topic as a guide?
It’s likely that the raycast is hitting the player’s accessories, the code looks fine from here.
Usually some accessories has a large mesh part, which can cause problems with raycasts that doesn’t exclude them from their filter lists.
What you’re currently doing, is creating a Raycast, waiting until a Tween is finished, then only after the tween has completed, checking the Raycast. Raycasting is useful for getting a hitpoint instantly from a direction. It’s kind of useless when you throw in a wait before using the hitpoint, as things can shift around in that timeframe.
What I would do, is get rid of the Raycast, and attach some sort of “touched event” to the fireball itself. Whether if you literally just use an .Touched connection, or create a GetPartBoundsInBox() loop that breaks when either a player’s part is detected, or the Tween completes.
Having this kind of physical check on the fireball itself allows it to be hit whenever, and would serve your use case well
I agree for the most part, except for using .Touched(), it’s only useful if you’re relying heavily on the object bouncing around unpredictable (i.e. certain weapons in Item Asylum). Given it’s a fire ball that also creates an explosion, rapid magnitude checks would work quite well.
Thats not what i mean. I mean after the ray fires if it hits a player it will send out the projectile and it will explode seemingly without touching anything if the player has moved because the ray’s end point hasn’t changed.