Sorry for bumping an old thread but, how come the bullet Hit detection for me doesn’t really work?
here’s the GIF here:
https://gyazo.com/97b8bfff5cc8ec0752c0af78b465935c
and here’s the script portion:
function OnRayHit(HitPart, HitPoint, Normal, Material, CosmeticBulletObject)
--This function will be connected to the Caster's "RayHit" event.
CosmeticBulletObject:Destroy() --Destroy the cosmetic bullet.
if HitPart and HitPart.Parent then --Test if we hit something
local Humanoid = HitPart.Parent:FindFirstChildOfClass("Humanoid") --Is there a humanoid?
if Humanoid then
Humanoid:TakeDamage(10) --Damage.
end
MakeParticleFX(HitPoint, Normal) --Particle FX
end
end
function OnRayUpdated(CastOrigin, SegmentOrigin, SegmentDirection, Length, CosmeticBulletObject)
--Whenever the caster steps forward by one unit, this function is called.
--The bullet argument is the same object passed into the fire function.
local BulletLength = CosmeticBulletObject.Size.Z / 2 --This is used to move the bullet to the right spot based on a CFrame offset
CosmeticBulletObject.CFrame = CFrame.new(SegmentOrigin, SegmentOrigin + SegmentDirection) * CFrame.new(0, 0, -(Length - BulletLength))
end
the hit detection is literally the same as the test weapon you have, but here’s the fire function, if there’s a problem with that.
local function shoot(Player, Direction)
--//Initialize
--if not canShoot(player, true) then return end
--shootSound:Play()
if tool.Parent:IsA("Backpack") then return end --Can't fire if it's not equipped.
--Note: Above isn't in the event as it will prevent the CanFire value from being set as needed.
if tool.Parent:IsA("Backpack") then return end --Can't fire if it's not equipped.
--Note: Above isn't in the event as it will prevent the CanFire value from being set as needed.
--We need to make sure the bullet inherits the velocity of the gun as it fires, just like in real life.
local HumanoidRootPart = tool.Parent:WaitForChild("HumanoidRootPart", 1) --Add a timeout to this.
local MyMovementSpeed = HumanoidRootPart.Velocity --To do: It may be better to get this value on the clientside since the server will see this value differently due to ping and such.
local ModifiedBulletSpeed = (Direction * BULLET_SPEED) + MyMovementSpeed --We multiply our direction unit by the bullet speed. This creates a Vector3 version of the bullet's velocity at the given speed. We then add MyMovementSpeed to add our body's motion to the velocity.
--Prepare a new cosmetic bullet
local Bullet = CosmeticBullet:Clone()
Bullet.CFrame = CFrame.new(FirePointObject.WorldPosition, FirePointObject.WorldPosition + Direction)
Bullet.Parent = workspace
Caster:Fire(FirePointObject.WorldPosition, Direction * BULLET_MAXDIST, ModifiedBulletSpeed, Bullet)
--We need to make sure the bullet inherits the velocity of the gun as it fires, just like in real life.
local HumanoidRootPart = tool.Parent:WaitForChild("HumanoidRootPart", 1) --Add a timeout to this.
local MyMovementSpeed = HumanoidRootPart.Velocity --To do: It may be better to get this value on the clientside since the server will see this value differently due to ping and such.
local ModifiedBulletSpeed = (Direction * BULLET_SPEED) + MyMovementSpeed --We multiply our direction unit by the bullet speed. This creates a Vector3 version of the bullet's velocity at the given speed. We then add MyMovementSpeed to add our body's motion to the velocity.
--Prepare a new cosmetic bullet
local Bullet = CosmeticBullet:Clone()
Bullet.CFrame = CFrame.new(FirePointObject.WorldPosition, FirePointObject.WorldPosition + Direction)
Bullet.Parent = workspace
Caster.LengthChanged:Connect(function (CastOrigin, SegmentOrigin, SegmentDirection, Length, CosmeticBulletObject)
CosmeticBulletObject.CFrame = CFrame.new(SegmentOrigin, SegmentOrigin + SegmentDirection)end)
Caster:Fire(FirePointObject.WorldPosition, Direction * BULLET_MAXDIST, ModifiedBulletSpeed, Bullet)
end