Hello, I’m working on a fps framework and used the FastCast Module.
local replicatedStorage = game:GetService("ReplicatedStorage")
local tweenService = game:GetService("TweenService")
local Events = replicatedStorage:WaitForChild("Events")
local Particles = replicatedStorage:WaitForChild("Particles")
local ShootingEvent= Events:WaitForChild("ShootingEvent")
local showBullet = Events:WaitForChild("showBullet")
local FastCast = require(script.FastCastRedux)
local DEBUG = true -- Whether or not to use debugging features of FastCast, such as cast visualization.
local BULLET_SPEED = 150 -- Studs/second - the speed of the bullet
local BULLET_MAXDIST = 1000 -- The furthest distance the bullet can travel
local BULLET_GRAVITY = Vector3.new(0, -50, 0) -- The amount of gravity applied to the bullet in world space (so yes, you can have sideways gravity)
local MIN_BULLET_SPREAD_ANGLE = 0 -- THIS VALUE IS VERY SENSITIVE. Try to keep changes to it small. The least accurate the bullet can be. This angle value is in degrees. A value of 0 means straight forward. Generally you want to keep this at 0 so there's at least some chance of a 100% accurate shot.
local MAX_BULLET_SPREAD_ANGLE = 0 -- THIS VALUE IS VERY SENSITIVE. Try to keep changes to it small. The most accurate the bullet can be. This angle value is in degrees. A value of 0 means straight forward. This cannot be less than the value above. A value of 90 will allow the gun to shoot sideways at most, and a value of 180 will allow the gun to shoot backwards at most. Exceeding 180 will not add any more angular varience.
local FIRE_DELAY = 0 -- The amount of time that must pass after firing the gun before we can fire again.
local BULLETS_PER_SHOT = 1 -- The amount of bullets to fire every shot. Make this greater than 1 for a shotgun effect.
local PIERCE_DEMO = true
--loading ray
local CosmeticBullet = Instance.new("Part")
CosmeticBullet.Material = Enum.Material.SmoothPlastic
CosmeticBullet.BrickColor = BrickColor.new("Royal purple")
CosmeticBullet.CanCollide = false
CosmeticBullet.Anchored = true
CosmeticBullet.Size = Vector3.new(0.2, 0.2, 2.4)
local Caster = FastCast.new()
local CastParams = RaycastParams.new()
CastParams.IgnoreWater = true
CastParams.FilterType = Enum.RaycastFilterType.Whitelist
local CastBehavior = FastCast.newBehavior()
CastBehavior.RaycastParams = CastParams
CastBehavior.MaxDistance = BULLET_MAXDIST
CastBehavior.HighFidelityBehavior = FastCast.HighFidelityBehavior.Default
CastBehavior.CosmeticBulletContainer = workspace.dummies
CastBehavior.Acceleration = BULLET_GRAVITY
CastBehavior.AutoIgnoreContainer = true
local TAU = math.pi * 2 -- Set up mathematical constant Tau (pi * 2)
local RNG = Random.new()
CastBehavior.CosmeticBulletTemplate = CosmeticBullet
ShootingEvent.OnServerEvent:Connect(function(player, direction,GunFirePoint)
local directionalCF = CFrame.new(Vector3.new(), direction)
local direction = (directionalCF * CFrame.fromOrientation(0, 0, RNG:NextNumber(0, TAU)) * CFrame.fromOrientation(math.rad(0), 0, 0)).LookVector
local character = player.Character or workspace:FindFirstChild(player.Name)
local humanoidRootPart = character: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.
local simBullet = Caster:Fire(GunFirePoint, direction, modifiedBulletSpeed, CastBehavior)
end)
Caster.LengthChanged:Connect(function(cast, segmentOrigin, segmentDirection, length, segmentVelocity, cosmeticBulletObject)
if cosmeticBulletObject == nil then return end
local bulletLength = cosmeticBulletObject.Size.Z / 1 -- This is used to move the bullet to the right spot based on a CFrame offset
local baseCFrame = CFrame.new(segmentOrigin, segmentOrigin + segmentDirection)
cosmeticBulletObject.CFrame = baseCFrame * CFrame.new(0, 0, -(length - bulletLength))
end)
function OnRayHit(cast, raycastResult, segmentVelocity, cosmeticBulletObject)
-- This function will be connected to the Caster's "RayHit" event.
local hitPart = raycastResult.Instance
local hitPoint = raycastResult.Position
local normal = raycastResult.Normal
if hitPart ~= nil and hitPart.Parent ~= nil then -- Test if we hit something
local humanoid = hitPart.Parent:FindFirstChildOfClass("Humanoid") -- Is there a humanoid?
if humanoid then
humanoid:TakeDamage(10) -- Damage.
end
end
end
function OnRayTerminated(cast)
local cosmeticBullet = cast.RayInfo.CosmeticBulletObject
if cosmeticBullet ~= nil then
-- This code here is using an if statement on CastBehavior.CosmeticBulletProvider so that the example gun works out of the box.
-- In your implementation, you should only handle what you're doing (if you use a PartCache, ALWAYS use ReturnPart. If not, ALWAYS use Destroy.
cosmeticBullet:Destroy()
end
end
Caster.CastTerminating:Connect(OnRayTerminated)
Caster.RayHit:Connect(OnRayHit) >
When i shoot at the sky it works the way i want it to. When i shoot at and object the projectile is going to one place. i have not even blacklisted or whitelisted anything.