CURRENT VERISON 1.2
SimpleCast seeks to be a simpler version of FastCast. FastCast and SimpleCast do not use roblox physics but simulate our own that is built in. With custom physics we can more arcuately project a projectile as it goes through 3D Space without all the lag. The API for moving a projectile is exposed in this module which allows you to project where your projectile may go before firing. (See Demo place for an example.) SimpleCast also has a Projectile Cache built in to help with optimizations. (Inspired from PartCache) SimpleCast also allows for CustomData to be added into each projectile. (See demo place for an example of this)
Module: SimpleCast [Version: 1.2] - Roblox
Demo Place: SimpleCast Demo Place - Roblox (Place has bouncing balls, and shows how you can visualize where a projectile will go. [check StarterGui and ReplicatedStorage]. Also has some basic replication. )
How to use:
--Local Script
--Require Simplecast and create Settings
local SimpleCast = require(game.ReplicatedStorage.SimpleCast)
--This is a 'predicate' or 'lambda' that is passed into settings and we can damage players or terminate the cast
local function onBallHit(self, hitObj: RaycastResult, castData: SimpleCast.CastData, newPosition: Vector3)
self:CastTerminate(castData)
return
end
local castSettings: SimpleCast.SimpleCastSettings = {
MaxLifeTime = 5, --LIFETIME IS NOT DISTANCE BUT ACTUALLY HOW LONG IT FLYS FOR
CacheSize = 50, --INIT CACHE SIZE
CacheGrowthSize = 10, --GROWTH SIZE
Gravity = GRAVITY, --GRAVITY CAN BE A VECTOR
RaycastParam = raycastParams, --ROBLOX'S RAYCASTPARMS
OnCastHit = onBallHit,
Data = {
--CUSTOM DATA
},
OnCastTerminate = function(castData: CastData) --FIRE ON CAST TERMINTE
print("custom data: " .. castData.Data)
--CLEAN UP OR RESET CUSTOM DATA
end
}
--Create or get castObject
local ball: BasePart = ...
--Create a caster
local caster = SimpleCast.new(ball, castSettings)
--Fire our caster when Button1Down is pressed
Mouse.Button1Down:Connect(function()
local rootPart = Player.Character.HumanoidRootPart
local mouseHit = Mouse.Hit + offset
local direction = (mouseHit.Position - rootPart.Position).Unit
local velo = direction * SPEED
local initPos = rootPart.Position
caster:FireCast(velo, initPos)
end)
API:
--GetPosition, GetVelocity and FastMag are static functions
GetPosition : (t: number, initVelo: Vector3, initPos: Vector3, gravity: Vector3) -> Vector3,
GetVelocity: (t: number, initVelo: Vector3, gravity: Vector3) -> Vector3,
FastMag: (vector: Vector3) -> number
new: (castObj: BasePart, simpleCastSettings: SimpleCastSettings) -> SimpleCastClass,
Raycast: (startPoint: Vector3, endPoint: Vector3) -> RaycastResult | nil,
FireCast: (initVelocity: Vector3, initPosition: Vector3, length: number) -> CastData,
CastTerminate: (castData: CastData) -> (), --PASS CastData to Terminate a projectile
OnCastMove: (self: any, customData: CastData, oldVec: Vector3, newVec: Vector3) -> ()
Destroy: () -> (),
SimpleCastSettings = {
MaxLifeTime: number,
CacheSize: number,
CacheGrowthSize: number,
Gravity: Vector3,
RaycastParam: RaycastParams,
OnCastHit: (self: any, hitObj: RaycastResult, castData: CastData, newPosition: Vector3) -> (),
CustomData: any,
OnCastTerminate: (customData: CastData) -> () --FIRE ON CAST TERMINTE
OnCastMove: (self: any, customData: CastData, oldVec: Vector3, newVec: Vector3)->(), FIRES AFTER CAST MOVES
}
UPDATE 1.2
- Casts are no longer tied to FPS
- Added OnCastMove, runs after cast is moved.
NOTES:
When the caster is created a Pre-Simulation event is connected.
GetPosition(t: number, initVelo: Vector3, initPos: Vector3, gravity: Vector3): Vector3 is the brains behind the movement of this module. GetPosition returns the position at a given time.
GetVelocity(t: number, initVelo: Vector3, gravity: Vector3): Vector3 is the derivative of GetPosition and will return the velocity at a given time.
FastMag(vector: Vector) is just vector.Magnitude but without the square root. Hence the name ‘fast mag’. It is never used in this module but I figured I would include it anyways.
For help on Raycastparms: RaycastParams | Roblox Creator Documentation
I am still working on a few things but overall the API will not change. I am welcome to any suggestions or criticism. I will put a GitHub repo sometime.
I currently use the same methods as PartCache to cache my bullets. I am trying to see if I can find a better way to do it but I think that any other method may be overkill and will not result in better performance that will matter.
If this does not meet your needs I recommend using FastCast @Xan_TheDragon put tons of work into it and it is a great module.