How to make the viewmodel not overlap on objects?

Hi, so I’m currently working on a game right now and the progress seems to be going well. The game also involves FPS framework so ViewModels are involved.

There’s this thing that’s been bothering me for a while now. You see when I clone the viewmodel and look at the air, it looks like this:

Now when I get too close to an object or something, it literally clips through the object:


I’m trying to find a way to make the ViewModel not overlap through objects.
You guys may already say to me that “Viewports is the solution”. It looks like a good solution but the problem is the rendering and lighting. ViewportFrames don’t have proper lighting and the viewmodel looks so weird cause the edges of it are pixelated, caused by the ViewportFrame.

I was wondering if you guys have any ideas by fixing this through code not through Viewports. Seriously, Viewports just make the viewModel look weird.

This is how the viewmodel is updated:

RunService.RenderStepped:Connect(function()
	if Shotgun then
		Shotgun:SetPrimaryPartCFrame(camera.CFrame * CFrame.new(1,0,1))
	end
end)

As you can see that’s a simple code for updating the viewModel’s CFrame. Again, if you have any ideas by fixing viewModel clipping through code, please share it.

Well, you’re gonna use ViewportFrames, and for me I’ll stick with it being on the camera, because ViewportFrame’s lighting won’t match with the environment

1 Like

Have you tried turning CanCollide on?

1 Like

@iceking_gfx If you turn CanCollide on for all parts, and you zoom into first person, you’ll start flying/flinging everywhere as you look around.

3 Likes

You can’t, short of making the player collider bigger to encompass the entire gun. Even games like phantom forces and arsenal have this problem

1 Like

I am uncertain as to how popular FPS games (Halo, COD, CS:GO, TF2) have viewmodels that don’t overlap with world objects.

1 Like

Games like that render it separately over the game, to my knowledge.
I dont think you can do this in roblox .

2 Likes

You can think of it as ViewportFrames that actually work.

1 Like

Other FPS games made with Unity/Unreal put the viewmodel in a camera that renders seperately from the rest of the world. Unfortunately, Roblox does not have that feature at this time

2 Likes

ROBLOX technically does, they have ViewportFrames for a reason.

1 Like

Not possible.
The closest thing you can get with is just simply using a gui instead of the gun which is what popular none roblox fps games have done.

But it’s just simple if you don’t mind it

1 Like

games like arsenal do that? or they just do it normal

1 Like

You can use a raycast on the front of barrel of the gun and check the distance of the wall, if it is near or not. if it is near take the gun back, just like real life.

2 Likes

Not Possible :frowning:

As others have started, it is impossible to make view models not overlap other objects in Roblox. The technology simply isn’t there yet.


How video game companies approach it:

What many shooter games like Overwatch and Valorant will do is create a separate camera that acts as its own overlay layer. This layer will overlay everything in the game world and is usually (if not always) client-sided. That’s why in Valorant if you stand against a wall with your gun pulled out your gun will seem tiny compared to the wall, it’s a whole separate scene.


Why it doesn’t work in Roblox:

You may be wondering, if it’s a whole separate scene why does that gun have the same lighting as the world? Well, unlike viewports in Roblox, many major game engines have systems in place to apply lighting to individual objects stored in separate scenes. So, once again, we’re defeated by the limitations of the Roblox engine.


Workarounds:

As for workarounds, you could make it so the gun is tucked away whenever it is about to collide with a wall using raycasts as @ShadowClaw_Dev mentioned.


Conclusion:

Most players, myself included, don’t mind the gun clipping through walls like that; it’s almost expected on Roblox. However, if you endeavor to find a workaround I wish you the best of luck! :slight_smile:

3 Likes

you can use RayCasting.

make a ray that points out the front of the gun, set the length to the desired length, when the ray gets intercepted, get the distance from the gun and where it was intercepted, then set the viewmodels offset backward so that it looks like it is getting pushed back, but you will probably need to figure out some math to get it working right.

im also currenly trying to do this for my game, but i cant get the math right, if i get it done and it works, ill reply again with it, but i might give up on it because it might cause problems and isnt really needed.

7 Likes

Strange how no one posted a solution to this day. There’s 2 ways you can deal with this:

  • Scale the viewmodel: make it very small and position it closely to the camera, you won’t see a difference visually and it’ll no longer clip through.

  • Use viewport frames: it’s rendered in a different workspace, hence will not clip through anything.

The problem with viewport frames right now is that the lighting is bad, so I prefer scaling.

2 Likes