Please watch the video below for what I’m saying to make sense
As you can see its a top down shooter game, very early in the works, anyways the player has a local script inside of starterplayerscripts rotating the root to the mouse hit, heres thats script:
local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local char = player.Character or player.CharacterAdded:Wait()
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local moveForwardsEvent = ReplicatedStorage.Movement.Forwards
local UIS = game:GetService("UserInputService")
local root = char:WaitForChild("HumanoidRootPart")
local RS = game:GetService("RunService")
local mouse = player:GetMouse()
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {char}
function cameraAdjust()
local Height = Vector3.new(0, 40, 0)
local rotation = CFrame.Angles(math.rad(-90), math.rad(0), math.rad(0))
camera.CFrame = CFrame.new(root.Position + Height) * rotation
end
mouse.Move:Connect(function()
local MousePos = mouse.Hit.Position
local direction = (MousePos - root.Position).unit
root.CFrame = CFrame.lookAt(root.Position, Vector3.new(mouse.Hit.X, root.Position.Y, mouse.Hit.Z))
end)
mouse.Button1Down:Connect(function()
local direction = root.Position - mouse.Hit.Position
local raycast = workspace:Raycast(root.Position, direction, raycastParams)
-- moveForwardsEvent:FireServer(true)
end)
mouse.Button1Up:Connect(function()
--moveForwardsEvent:FireServer(false)
end)
RS:BindToRenderStep("Camera", 210, cameraAdjust)
The script using FireCast is this
local tool = script.Parent
local shootEvent = script.Parent:WaitForChild("Shoot")
local BulletsFolder = game.Workspace:WaitForChild("Bullets")
local FastCast = require(game.ReplicatedStorage.FastCastRedux)
-- FastCast.VisualizeCasts = true
local castBehavior = FastCast.newBehavior()
local castParams = RaycastParams.new()
castParams.FilterType = Enum.RaycastFilterType.Exclude
castParams.IgnoreWater = true
local bulletTemplate = Instance.new("Part")
bulletTemplate.Anchored = true
bulletTemplate.CanCollide = false
bulletTemplate.Size = Vector3.new(0.1, 0.1, 4)
bulletTemplate.Material = Enum.Material.Neon
bulletTemplate.BrickColor = BrickColor.new("Yellow flip/flop")
castBehavior.RaycastParams = castParams
castBehavior.CosmeticBulletContainer = BulletsFolder
castBehavior.CosmeticBulletTemplate = bulletTemplate
local function Hit(cast, result, velocity, bullet)
local hit = result.Instance
local character = hit:FindFirstAncestorWhichIsA("Model")
if character and character:FindFirstChild("Humanoid") then
character.Humanoid:TakeDamage(20)
end
if bullet then
bullet:Destroy()
end
end
local function onLengthChanged(cast, lastPoint, direction, length, velocity, bullet)
if bullet then
local bulletLength = bullet.Size.Z / 2
local offset = CFrame.new(0, 0, -(length - bulletLength))
bullet.CFrame = CFrame.lookAt(lastPoint, lastPoint + direction):ToWorldSpace(offset)
end
end
local function equipped()
castParams.FilterDescendantsInstances = {tool.Parent, BulletsFolder}
end
local caster = FastCast.new()
local function shoot(player, mousePosition)
local firePoint = script.Parent.Barrel.FirePoint
local origin = firePoint.WorldPosition
local direction = player.Character.HumanoidRootPart.CFrame.LookVector.Unit
caster:Fire(origin, direction * 100, 100, castBehavior)
end
tool.Equipped:Connect(equipped)
shootEvent.OnServerEvent:Connect(shoot)
caster.LengthChanged:Connect(onLengthChanged)
caster.RayHit:Connect(Hit)
If the player rotates too fast the bullet will be delayed, not firing from the tip of guns barrel, as it should.
Help is appreciated
1 post - 1 participant