Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local DEBUG = true -- Whether or not to use debugging features of FastCast, such as cast visualization.
- local BULLET_SPEED = 100 -- Studs/second - the speed of the bullet
- local BULLET_MAXDIST = 1000 -- The furthest distance the bullet can travel
- 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)
- 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 = 2 -- 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 = false -- True if the pierce demo should be used. See the CanRayPierce function for more info.
- local RNG = Random.new() -- Set up a randomizer.
- local TAU = math.pi * 2 -- Set up mathematical constant Tau (pi * 2)
- local PartCacheModule = require(game.ReplicatedStorage.PartCache)
- --
- local CosmeticBulletsFolder = workspace:FindFirstChild("CosmeticBulletsFolder") or Instance.new("Folder", workspace)
- CosmeticBulletsFolder.Name = "CosmeticBulletsFolder"
- local excludeTable = {workspace.enemyStorageDeBug, CosmeticBulletsFolder}
- local FastCast = require(game.ReplicatedStorage.FastCastRedux)
- local Caster = FastCast.new()
- Caster.VisualizeCasts = true
- Caster.DebugLogging = false
- local Pencil = workspace.Map.Pencil:Clone()
- local Handle = Pencil.Handle
- local firingPoint = Pencil.Position
- local CosmeticPartProvider = PartCacheModule.new(Pencil, 100, CosmeticBulletsFolder)
- local castParams = RaycastParams.new()
- castParams.FilterDescendantsInstances = excludeTable
- castParams.FilterType = Enum.RaycastFilterType.Exclude
- local castBehavior = FastCast.newBehavior()
- castBehavior.Acceleration = BULLET_GRAVITY
- castBehavior.CosmeticBulletContainer = CosmeticBulletsFolder
- castBehavior.RaycastParams = castParams
- castBehavior.AutoIgnoreContainer = false
- castBehavior.CosmeticBulletProvider = CosmeticPartProvider
- function OnRayHit(cast, castResult, segmentVelocity, cosmeticBulletObject)
- -- This function will be connected to the Caster's "RayHit" event.
- local hitPart = castResult.Instance
- print(hitPart.Name)
- local hitPoint = castResult.Position
- local normal = castResult.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(15) -- Damage.
- end
- end
- end
- function OnRayUpdated(cast, lastPoint, direction, length, velocity, bullet)
- -- 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 bullet then
- local bulletLength = bullet.Size.Z / 2 -- This is used to move the bullet to the right spot based on a CFrame offset
- local offset = CFrame.new(0, 0, -(length - bulletLength))
- bullet.CFrame = CFrame.lookAt(lastPoint, lastPoint + direction):ToWorldSpace(offset)
- --bulletTemplate.CFrame.
- 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.
- if castBehavior.CosmeticBulletProvider ~= nil then
- castBehavior.CosmeticBulletProvider:ReturnPart(cosmeticBullet)
- else
- cosmeticBullet:Destroy()
- end
- end
- end
- Caster.LengthChanged:Connect(OnRayUpdated)
- Caster.RayHit:Connect(OnRayHit)
- Caster.CastTerminating:Connect(OnRayTerminated)
- function PencilThrow()
- print("pencil trhown")
- firingPoint = script.Parent.Pencil.Position
- local direction = (nearestPlayer.Character.HumanoidRootPart.Position - firingPoint).Unit * maxDistance
- local directionalCFrame = CFrame.new(Vector3.new(), direction)
- local direction2 = (directionalCFrame * CFrame.fromOrientation(math.rad(RNG:NextNumber(MIN_BULLET_SPREAD_ANGLE, MAX_BULLET_SPREAD_ANGLE)), 0, 0)).LookVector
- Caster:Fire(firingPoint, direction2, BULLET_SPEED, castBehavior)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement