Advertisement
fishinthebox

Current FastCast Script

Aug 12th, 2024
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.95 KB | None | 0 0
  1. local DEBUG = true          -- Whether or not to use debugging features of FastCast, such as cast visualization.
  2. local BULLET_SPEED = 100    -- Studs/second - the speed of the bullet
  3. local BULLET_MAXDIST = 1000                         -- The furthest distance the bullet can travel
  4. local BULLET_GRAVITY = Vector3.new(0, -workspace.Gravity, 0)        -- The amount of gravity applied to the bullet in world space (so yes, you can have sideways gravity)
  5. 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.
  6. 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.
  7. local FIRE_DELAY = 2                    -- The amount of time that must pass after firing the gun before we can fire again.
  8. local BULLETS_PER_SHOT = 1                          -- The amount of bullets to fire every shot. Make this greater than 1 for a shotgun effect.
  9. local PIERCE_DEMO = false                           -- True if the pierce demo should be used. See the CanRayPierce function for more info.
  10. local RNG = Random.new()                            -- Set up a randomizer.
  11. local TAU = math.pi * 2                         -- Set up mathematical constant Tau (pi * 2)
  12. local PartCacheModule = require(game.ReplicatedStorage.PartCache)
  13.  
  14. --
  15.  
  16. local CosmeticBulletsFolder = workspace:FindFirstChild("CosmeticBulletsFolder") or Instance.new("Folder", workspace)
  17. CosmeticBulletsFolder.Name = "CosmeticBulletsFolder"
  18.  
  19. local excludeTable = {workspace.enemyStorageDeBug, CosmeticBulletsFolder}
  20.  
  21. local FastCast = require(game.ReplicatedStorage.FastCastRedux)
  22. local Caster = FastCast.new()
  23. Caster.VisualizeCasts = true
  24. Caster.DebugLogging = false
  25. local Pencil = workspace.Map.Pencil:Clone()
  26. local Handle = Pencil.Handle
  27.  
  28. local firingPoint = Pencil.Position
  29.  
  30. local CosmeticPartProvider = PartCacheModule.new(Pencil, 100, CosmeticBulletsFolder)
  31.  
  32. local castParams = RaycastParams.new()
  33. castParams.FilterDescendantsInstances = excludeTable
  34. castParams.FilterType = Enum.RaycastFilterType.Exclude
  35.  
  36.  
  37. local castBehavior = FastCast.newBehavior()
  38. castBehavior.Acceleration = BULLET_GRAVITY
  39. castBehavior.CosmeticBulletContainer = CosmeticBulletsFolder
  40. castBehavior.RaycastParams = castParams
  41. castBehavior.AutoIgnoreContainer = false
  42. castBehavior.CosmeticBulletProvider = CosmeticPartProvider
  43.  
  44. function OnRayHit(cast, castResult, segmentVelocity, cosmeticBulletObject)
  45.     -- This function will be connected to the Caster's "RayHit" event.
  46.     local hitPart = castResult.Instance
  47.     print(hitPart.Name)
  48.     local hitPoint = castResult.Position
  49.     local normal = castResult.Normal
  50.     if hitPart ~= nil and hitPart.Parent ~= nil then -- Test if we hit something
  51.         local humanoid = hitPart.Parent:FindFirstChildOfClass("Humanoid") -- Is there a humanoid?
  52.         if humanoid then
  53.             humanoid:TakeDamage(15) -- Damage.
  54.         end
  55.     end
  56. end
  57.  
  58. function OnRayUpdated(cast, lastPoint, direction, length, velocity, bullet)
  59.     -- Whenever the caster steps forward by one unit, this function is called.
  60.     -- The bullet argument is the same object passed into the fire function.
  61.     if bullet then
  62.         local bulletLength = bullet.Size.Z / 2 -- This is used to move the bullet to the right spot based on a CFrame offset
  63.         local offset = CFrame.new(0, 0, -(length - bulletLength))
  64.         bullet.CFrame = CFrame.lookAt(lastPoint, lastPoint + direction):ToWorldSpace(offset)
  65.         --bulletTemplate.CFrame.
  66.     end
  67. end
  68.  
  69. function OnRayTerminated(cast)
  70.     local cosmeticBullet = cast.RayInfo.CosmeticBulletObject
  71.     if cosmeticBullet ~= nil then
  72.         -- This code here is using an if statement on CastBehavior.CosmeticBulletProvider so that the example gun works out of the box.
  73.         -- In your implementation, you should only handle what you're doing (if you use a PartCache, ALWAYS use ReturnPart. If not, ALWAYS use Destroy.
  74.         if castBehavior.CosmeticBulletProvider ~= nil then
  75.             castBehavior.CosmeticBulletProvider:ReturnPart(cosmeticBullet)
  76.         else
  77.             cosmeticBullet:Destroy()
  78.         end
  79.     end
  80. end
  81.  
  82. Caster.LengthChanged:Connect(OnRayUpdated)
  83. Caster.RayHit:Connect(OnRayHit)
  84. Caster.CastTerminating:Connect(OnRayTerminated)
  85.  
  86. function PencilThrow()
  87.     print("pencil trhown")
  88.     firingPoint = script.Parent.Pencil.Position
  89.     local direction = (nearestPlayer.Character.HumanoidRootPart.Position - firingPoint).Unit * maxDistance
  90.     local directionalCFrame = CFrame.new(Vector3.new(), direction)
  91.     local direction2 = (directionalCFrame * CFrame.fromOrientation(math.rad(RNG:NextNumber(MIN_BULLET_SPREAD_ANGLE, MAX_BULLET_SPREAD_ANGLE)), 0, 0)).LookVector
  92.     Caster:Fire(firingPoint, direction2, BULLET_SPEED, castBehavior)
  93. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement