jaipack17 / 2D-Projectile-Module

a module to create 2 dimensional gui projectiles on roblox

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

yzLh92Vqxh.mp4

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!

diagram

gcMrA3wwhe.mp4

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:

yzLh92Vqxh.mp4

✌️

About

a module to create 2 dimensional gui projectiles on roblox

License:MIT License


Languages

Language:Lua 100.0%