[Deprecated] 2D Projectile Module - An easy way to create dynamic 2D projectiles

2D Projectile Module

Documentation | Source Code | Module | Playground

About

Using this module, you can create dynamic projectiles for your 2 dimensional GUI-Based Roblox games! No matter what the rotation of the weapon is, you are able to shoot projectiles in the direction the weapon is facing to! This module is really helpful for people trying to make 2 Dimensional games on Roblox!

Module

For instance, A Rocket Launcher:

How does it work?

Whenever a user interacts with their device (Custom Interactions can be scripted), a thin frame (ray) is created in the front face of a given Gui Object according to its rotation and relative to the Gui Object’s size. A Projectile (bullet) is then created inside of that ray (relative to the ray’s size) and is then tweened along the path of the ray, which you observe as moving projectiles on you screen!

Examples:

Our Weapon:

image
image

“Bulk” will be the frame that will be rotated.

image

“Weapon Controller” will be the script we’ll be coding in! The code below utilizes all the functions and events of the module!

local Module = require(path.to.module)
local weapon = Module.new(script.Parent.Parent.Bulk, script.Parent.Parent.Bulk) -- initializing our weapon. the first argument is the frame that will be rotating, the second argument is the frame, in which the ray will emerge from.

weapon:SetDestroy(true) -- destroy after the projectile has reached the end point? yes

weapon:SetRayLength(5) -- the length of the ray thats formed (relative to the frame it is parented to)

weapon:SetBulletSize(UDim2.new(0.05, 0, 10, 0)) -- set projectile size (relative to the ray's size)

weapon:SetBulletColor(Color3.new(255,255,0)) -- set the color of the bullet

weapon:SetRayVisibility(false) -- should the ray be visible when we shoot the projectile? no

weapon:SetTweenEasingStyle(Enum.EasingStyle.Linear) -- tween easing style

weapon:SetTweenLength(1) -- tween length
-- weapon:SetBullet(customBulletInstance) || used to create custom projectiles, lets say, an imagelabel, (size of the bullet will be affected by :SetBulletSize())

weapon:SetCallbackFunction(function() -- callback function (called when the projectile reaches the end point of the ray)
	print("Bullet has reached the end point!")
end)

game:GetService("UserInputService").InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		weapon:Shoot() -- shoot the projectile!
	end
end)

weapon.OnShot.Event:Connect(function() -- OnShot event, fired when the projectile is shot!
	print("OnShot event fired!")
end)

To rotate the Weapon, lets try out the following code, this will make the weapon follow the direction of the mouse! (debugging purposes)

In the same script:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local WeaponFrame = script.Parent.Parent.Bulk

game:GetService("RunService").Stepped:Connect(function()
	local frameCenter = WeaponFrame.AbsolutePosition + (WeaponFrame.AbsoluteSize/2)
	local x = math.atan2(mouse.Y - frameCenter.Y, mouse.X - frameCenter.X)
	WeaponFrame.Rotation = math.deg(x) + 90
end)

Result:


Links

Get the Module

Documenation

I hope this module helps all the people out there trying to make 2 dimensional games on roblox.

If you have any questions, queries or feedback, be sure to reply down below! It will be highly appreciated!

If you have any other suggestions, or new gui physics you would like to see, be sure to DM me on DevForum or reply below!

Cheers :v:

20 Likes

If you have any questions, queries or feedback, be sure to reply down below! It will be highly appreciated!

3 Likes

Documentation

Documentation is out, check it out here!


2 Likes

Heres something I created with the module! You can too!

9 Likes