Projectile Gun - How To..?

Im intrested in making a game that contains shooting mechanics but Im not too familiar with roblox’s new physics… and BodyVelocity being deprecated. :expressionless:

If you can tell me whats wrong with it so it doesnt apply forces that would be splendid!

Issue: Projectile Spawns in Origin of world without any Velocity.

Expected Behavior: Projectile spawns at the “Barrel” attachment’s position and flys off with Speed Velocity, To a Point derived from Target.X and Target.Z because this game is a top down. and the Y should be the players HumanoidRootPart’s Y Value.

Hierachy / Explorer:

image

Server:

local Tool = script.Parent
local Event = Tool:WaitForChild("Shoot")
local Config = Tool:WaitForChild("Config")

Event.OnServerEvent:Connect(function(Player, Target)
	local Character = Player.Character or Player.CharacterAdded:Wait()
	local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
	
	local Projectile = Instance.new("Part", workspace.Projectiles)
	Projectile.Shape = Enum.PartType.Ball
	Projectile.BrickColor = BrickColor.new("White")
	Projectile.Material = Enum.Material.Neon
	Projectile.CanCollide = false
	Projectile.Anchored = false
	
	Projectile.Size = Vector3.new(.3,.3,.3)
	Projectile.Position = Tool.Handle.Barrel.Position
	
	local Velocity = Instance.new("BodyVelocity", Projectile)
	Velocity.Velocity = Vector3.new(Target.X, HumanoidRootPart.Position.Y, Target.Z) * Config.Speed.Value
	Velocity.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
	
	game.Debris:AddItem(Projectile, 5)
end)

Client:

local Tool = script.Parent
local Event = Tool:WaitForChild("Shoot")

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

Tool.Activated:Connect(function()
	Event:FireServer(Mouse.Hit)
end)

I have confirmed Speed is set to a Valid Number. The goal is to use Non Dated Roblox Physics!

1 Like

Set the projectile’s AssemblyLinearVelocity to your desired velocity.

A few sidenotes:

  • It would probably be more performant to just use a series of raycasts and only have the projectile as a visual with something like FastCast

Parenting the object before setting its properties has a performance penalty, as the server has to deal with replicating it to the client twice instead of once.

2 Likes

If the gun’s bullet is not affected by gravity for a long range then the best thing to do is to use tweenservice and tween said projectile from start to end. IF it is affected by gravity simply use :ApplyImpulse or a linear velocity for a second or two.

2 Likes

Some guns just set the bullet CFrame forwards, using it’s LookVector, but if you want it to be physics-based, then yes, use AssemblyLinearVelocity.

2 Likes

For attachments use Barrel.WorldPosition since its position is relative to the parent part.

Velocity is a direction vector not a position valur.

You can get the direction to a position vector using (pos1 - pos2).Unit where pos1 is the target position and pos2 is the starting position. Unit is there to make this direction vector length one so that when you multiple the speed value its magnitude will be the same as the speed value.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.