So if you have ever play Fortnite, you will definitely notice that there’s a hit indicator UI in shotguns, indicating how many pellets hit a player’s character model.
(The red dots inside the crosshair)
I’m trying to recreate this in Roblox by using viewport frames, initially I have a code that creates bullet holes:
local hit,pos,norm = workspace:FindPartOnRayWithIgnoreList(ray,ignored)
...
local BulletHolePart = Instance.new("Part",workspace)
...
BulletHolePart.Name = "BulletHole"
BulletHolePart.Size = Vector3.new(0.5,0.5,0.05)
BulletHolePart.CFrame = CFrame.new(pos,pos+norm)
...
I’m trying to put the bullet holes that hit the player into a ViewportFrame, but how do I efficiently position the bullet hole parts to the ViewportCamera?
local VPC = Instance.new("Camera",MainUI.ViewportFrame)
gunUI.ViewportFrame.CurrentCamera = VPC
VPC.CFrame = CFrame.new(Vector3.new(0,2,0),BulletHolePart.Position)
I tried to manually do it in Studio and here’s the anticipated effect:
You can do pretty much the same thing with just simple parts… You will of course have to account for the spread of the Shotgun. But for a rifle, it would just be…
--Local Script
local chr = game.Players.LocalPlayer.Character
local Ray = Ray.new(GunTip, GunTip.CFrame.LookVector*Range)
local part,pos = workspace:FindPartOnRay(Ray, chr)
if part then
local ShotDisplay = Instance.new("Part", workspace)
ShotDisplay.Anchored = true
ShotDisplay.Transparency = .5
ShotDisplay.CanCollide = false
ShotDisplay.Color = Color3.new(255/255,0,0)
ShotDisplay.Size = Vector3.new(.1,.1,.1)
ShotDisplay.CFrame = CFrame.new(pos)
end
Keep in mind, this is just off the top of my head. It Should work in theory. To do this for a shotgun it will simple just be running this in a For loop then account for the spread on each one.
Note: You will have to make a new ray each time with the spread accounted for…
No, that’s not my actual question…
My question is how do I clone the bullet parts into the ViewportFrame, not how to create a shotgun or create bullet holes.
You can try using WorldToScreenPoint, which returns the hit point as a Vector2.
Here’s the wiki for WorldToScreenPoint
So basically you’d just need to return the hitpoint (Vector3) of the bullet for each bullet and pass it into that function, and then convert it into a vector2 value. You then can convert it into a UDim2 value by using the vector2 values (which are offset) like this: UDim2.new(0, Vector2.X, 0, Vector2.Y)
Do you mean how to set the bullet frame position to the Vector2 value?
If so, you could probably do it like this: UIBullet.Position = UDim2.new(0,vector.X, 0, vector.Y)