I need help with my viewmodel clipping into walls

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make my viewmodel move back a little bit using raycasts to prevent my viewmodel clipping into walls

  2. What is the issue? Include screenshots / videos if possible!
    I don’t know how to achieve such an effect I’m using Bind To render step for most of my major actions so im assuming if I was to use raycast I would need it to update constantly.

Code:

  1. What solutions have you tried so far? Did you look for solutions on the Creator Hub?
    I went looking for solutions on the forum but I can’t find one that explains how to achieve it well

Edit: There are a few notes there, thats for my team

Here’s a potential solution:
Raycast forward x amount of studs (change per gun), and then if the ray hits an object, either play an animation or apply a rotation offset to the viewmodel’s CFrame.

local viewmodelCFrame = ...

--// use rayParams, only collide with the map
local ray = workspace:RayCast(camera.CFrame.Position, camera.CFrame.LookVector * gunLength)

if ray then
    --// Apply offset, either through animation or changing Yaw, Pitch and Roll.
    viewmodelCFrame *= ... --// Better if you lerp a value, but this is a straight forward fix
end

viewmodel.CFrame = viewmodelCFrame

Additionally to that, if you’d want the animation to be a little more “real”, you could make the amount of rotational offset thats applied to the viewmodel base itself off how long the ray is, near max distance = barely any rotation, near min distance = almost all rotation.

local effectiveness = ray.Distance / gunLength - 1

... * effectiveness

I’m not the smartest so when there is a a ray what values would I lerp? the CFrame position of the actual viewmodel?

thank you it worked with the effectiveness would I do.

Viewmodel:PivotTo(workspace.CurrentCamera.CFrame * viewmodelCF * effectivness)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.