I’m working on a gun which fires a projectile looking towards the mouse. How can I make it so that the projectile is unaffected by gravity and moves in a constant speed towards the specified direction until I destroy it? Here’s the code to my gun so far:
local redGun = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local ServerControl = Instance.new("RemoteFunction")
ServerControl.Name = "ServerControl"
ServerControl.Parent = redGun
local ClientControl = Instance.new("RemoteFunction")
ClientControl.Name = "ClientControl"
ClientControl.Parent = redGun
local function onActivate()
local player = Players:FindFirstChild(redGun.Parent.Name)
print(player)
ClientControl:InvokeClient(player)
end
local function fireGun(user, mousePos)
local hum = redGun.Parent:FindFirstChild("HumanoidRootPart")
local direction = (hum.Position - mousePos).Unit
local xOffset = direction.X * -10
local zOffset = direction.Z * -10
local spawnPos = hum.Position + Vector3.new(xOffset, hum.Position.Y - 3, zOffset)
local gunParticle = ReplicatedStorage.WeaponParticlesNStuff.RedGunParticle:Clone()
local targetPos = Vector3.new(mousePos.X, hum.Position.Y , mousePos.Z)
gunParticle.Parent = workspace
gunParticle.CFrame = CFrame.new(hum.Position, targetPos)
gunParticle.Position = spawnPos
end
ClientControl.OnServerInvoke = fireGun
redGun.Activated:Connect(onActivate)
gunParticle.CFrame += gunParticle.CFrame. LookVector
will move it 1 magitude to the direction its facing you can put it in a loop t = game.TweenService:Create(gunParticle, TweenInfo.new(3, Enum.EasingStyle.Linear), {["Position"] = targetPos}) t:Play()
will tween the bulet so it reach target in 3 sec
to make it not effected by gravity anchor it
i’d recommend using fastcast for projectiles however if u wish to make your own what i’d do is use runservice to constantly update the bullets position in the direction it was fired an make a modification to the y axis (so it has gravity) but if u want realistic bullet physics use fastcast
What’s the problem with using FastCast? A tool is there to ease beginners and experienced developers.
According to the Roblox Documentations,
The Heartbeat event fires every frame, after the physics simulation has completed. The deltaTime argument indicates the time that has elapsed since the previous frame.
Here’s the order of the steps.
Your best bet is to use runService.PreRender or runService.RenderStepped in replacement of the Heartbeat.
That is a bad thinking. These known modules have been maintained by the scripter and contributed to by the community to ensure finest optimization and stability when integrating into the workflow. You should only make systems without relying on existing scripts if you are experimenting with them and can make a system better than the already existing one through analyzing the time difference and estimating accuracy.
If you were making private project, I understand, but it seems like you need something polished for your game that will be played by others that will otherwise be unstable.
I just wanted to add this but this guy madea great tutorial which kinda mixes cframes with physics, it is kinda hard to understand but works like a charm.