Bullet hole in ViewportFrames?

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.

image
(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:

Or is there any better way to create such effect but not using ViewportFrame?

2 Likes

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…

–12904

1 Like

But why Viewports? Correct me if I’m wrong, but wouldn’t a normal GUI be able to get the desired result with just a couple of frames and .visible?

3 Likes

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.

1 Like

https://gyazo.com/dae7fbd0fdf698c4ffceaeb9d7b3b34b

Sorry must of misunderstood your question… Although that example should give the same result you had with the viewport frames…

1 Like

Frames in normal GUI can solve but I have no idea how to position them

1 Like

That’s okay, For “Such effect”, I mean this:
image

1 Like

Well for my shotgun, the pattern is random everytime…

1 Like

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)

Hope this helps :slight_smile:

6 Likes

What does the vector3 value represents? Position?

Yep, so the ending position of where the bullet landed.

1 Like

I did print(vector.X), how do I assign it to a Frame’s Position?

1 Like

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)

2 Likes

Thanks! Even though I did not use viewport frame at last, but using WorldToScreenPoint actually works! Exactly what I wanted, thanks for helping :smiley:

https://gyazo.com/7255f6a9cd2e6bc3dc4f74bcec39984e

5 Likes