Making a combat game with ranged weapons? FastCast may be the module for you!

Is there a way to make a homing type projectile
This is basicly what i have going on but the Raycast is not getting updated so it jerkes around when it finds the target

function OnRayUpdated(cast, segmentOrigin, segmentDirection, length, segmentVelocity, 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.
if cosmeticBulletObject == nil then return end
local bulletLength = cosmeticBulletObject.Size.Z / 2 -- 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))
local t = cast.UserData["Target"]
local c = cast.UserData["Player"]

if(t == nil) then
	for i,v in pairs(workspace:GetDescendants()) do
		if(v:findFirstChild("Humanoid") and math.abs((v.HumanoidRootPart.Position - cosmeticBulletObject.Position).Magnitude) <= 15) then
			cosmeticBulletObject.CFrame = CFrame.new(cosmeticBulletObject.Position,v.HumanoidRootPart.Position)
			print(v)
		end
	end
end

This is because the container is the workspace, And since fastcast castbehaviour has a setting called “AutoIgnoreContainer” (True by default) it will ignore every descendant of the workspace.

1 Like

does anyone have an idea how i stop the bullet mid process? like a time stop stopping every velocity

This is a very useful module, but I want the effects done on the client while the actual hit is done on the server. Is there any way I should go about achieving that?

Hey, very nice api, extremely useful, however, I have a question about the projectiles. How would I add an effect to the bullet after the shot is fired, such as a trail? I’ve tried looking at the documentation but can’t seem to find anything but that’s probably just me being stupid. I’ve also tried adding it in the Fire function yet it makes a weird visual.

No you do not.

Personally, I have one caster in the server to handle the damage, and one on the client to handle the physical bullet.

All the client has is one ModuleScript that detects when the player equips a tool with LocalToolEquipped and binds a function to input with BindAction.

Here’s some of the client code to give you a rough idea:

local function onShoot(ActionName, InputState, InputObject, Gun)
	if ActionName == "Shoot" and InputState == Enum.UserInputState.Begin then
		print("Shooting.")
		ShootRemote:FireServer(Gun, Mouse.Hit.Position)
	end
end

-- Connected through a .OnClientEvent event
local function onReplicateCalled(Gun, MousePosition)
	local StartPart = Gun.Barrel --Depends on the gun
	
	local Direction = (MousePosition - StartPart.Position).Unit
	FastCastBehaviour.Acceleration = Vector3.new(0, -Gun.Gravity.Value, 0)
	ClientCaster:Fire(StartPart.Position, Direction, Gun.Power.Value,  FastCastBehaviour)
end

--Connected through a .LocalToolEquipped event
local function onToolEquipped(Weapon)
	print("Tool equipped!")
	ContextActionService:BindAction("Shoot", function(N, I, IO)
		onShoot(N, I, IO, Weapon)
	end, false, Enum.UserInputType.MouseButton1)
end

I also proceed to unbind the input when the tool is unequipped, and I detect this with LocalToolUnequipped.

On the server:

--Connected through a .OnServerEvent event
local function onShoot(Player, Gun, MousePosition)
	local RaycastParameters = createRaycastParams(Player)
	local OriginPart = Gun.Flare
	
	local Direction = (MousePosition - OriginPart.Position).Unit
	
	local FastCastBehaviour = FastCastRedux.newBehavior()

    --You can add checks for particular guns which shoot differently (eg. bombs that can also damage the player for particular RaycastParameters)
	FastCastBehaviour.RaycastParams = RaycastParameters
	FastCastBehaviour.Acceleration = Vector3.new(0, -Gun.Gravity.Value, 0)

	UniversalCaster:Fire(OriginPart.Position, Direction, Gun.Power.Value,  FastCastBehaviour)
	
    -- Merely preference.
	ShootRemote:FireAllClients(Gun, MousePosition)
end

I have this function that is called every time the Shoot Remote is fired to the server.

That adds up to two casters in the entire game.
I believe this also answers @OminousVibes0’s question.

3 Likes


Would be useful if there is an attraction property

AttractedPosition = Vector3
-The instance the trajectory is trying to reach
AttractionStrength = 0 to 1
-The strength of reaching the desired position

1 Like

Do you have any links to where I can look more into client replication for simulated physics?

Is it possible to make a part (or just find the position) where the cast lands? I’m sorry if this is a dumb question.

yes. Its an argument of the raycastresult in the RayHit event of the caster.

1 Like

Not yet. I want to though, I will make that known.

This is just an example of what I did

I see, but I have a couple questions

Why fire the cast on the server, if you want to visualize it on the client? Doesn’t the caster:Fire() method also create a projectile because it requires the castbehavior parameter, which passes the cosmeticbulletprovider? In the module, the ‘SimBullet’ function creates a cosmeticbulletobject. You are calling that on the server. How would that work?

The cast on the server is used mainly for hit detection. Of course, you could have the client do hit detection if you wanted and have the server do a valid check. The server doesn’t create the cosmetic bullet it only creates the ray. The clients create the visual stuff. Anything visual-related, aside from the debug, is removed from the server. You do not need to pass ‘CastBehavior.CosmeticBulletProvider’ on the server. In fact, FastCast still works without it.

oh sweet! Thats what I was worried about when I was reading the fastcast module. I couldn’t find anywhere, where the module checks if the cosmeticbulletprovider/ cosmetic bullet exists

I did some testing, If you are shooting and die while shooting (With the debug gun and my own) The bullets get frozen in the air, and will not be removed. How do you solve this?

Fast cast debug gun lowers Hearbeat to 14 am I doing somthing wrong?

By not having the script simulating the shots in the tool. When you die, it deletes the tool instance and consequently deletes the FC script too. That causes all simulation to completely stop and leave the cosmetic bullets behind.

1 Like

Mannnn, I wish I would have tried that! Spent hours looking at part cache, Thank you so much!

I am really proud of this module!
However, the only problem I am having with it is the fact that my bullets, when they have reached their max distance, just stay in the air and not get teleported (I am using Part Cache). Is there a way to work around this?

Perhaps I can use “GetVelocity()” for every bullet and send them far away as soon as they reach zero but I don’t know how I would implement that and as your website says, it will cause performance problems and I don’t want that in my game.