0
\$\begingroup\$

I am working on a game character using Unity. I'd like it to be playable in 3rd Person and 1st Person depending on what the user chooses (switchable in game whenever ideally).

I've worked on and read many examples all using different techiniques (some don't have any mesh for the player and just have a gun; others have no gun at all, just aiming; some use basic geometry only like cylinder and sphere bullet and dont really work when i try using high-poly human like etc).

So I am trying my own ideas and feel like maybe it's impossible for me to make a properly decent game character :[

Here is exactly what I have so far:

a) Mixamo 'Passive_Marker_Man' with idle-rifle-aiming animation.

b) SetIKPosition/Weight for each hand to specific transforms on the gun object.

c) Guns 'butt'/base is the gun objects pivot/origin.

d) The gun is spawned at a transform i made near the models right shoulder

e) Added child object Sphere to player 10 units forward on Z axis

f) Mouse movement on Y moves gun/sphere up and down; mouse X rotates entire player object left/right.

g) SetLookAtPosition IK also done to make head always look at sphere (gun is also always pointed at sphere)

h) camera is set to a position between the two eyes on every frame, and is set to lookAt the sphere.

......When i view this from a 3rd Person perspective it looks pretty good, the guys eyes follow his guns aim, which is pointing nicely at the sphere 10m away.

But if I view it in 1st Person, I can see inside the gun and parts are chopped off etc (aka clipping????)

So am I going about this all the wrong way. It felt so right at some points but now I can't imagine how to stop this kind of thing from ruining the character.

enter image description here

\$\endgroup\$
2
\$\begingroup\$

The way this is often done is to render in two passes:

  • first, render your scene with your normal near & far planes.

  • next, clear the depth buffer, and render your character's visible first-person limbs/tools using a very close near & far plane.

That way you can tune the near plane close enough to not clip the limbs, without damaging the depth precision you have available for rendering a large scene.

It also has the benefit that if you walk so close to a wall that your arm/tool would clip into it, the arm/tool is instead rendered on top (strictly incorrect, but looks better than seeing the geometry intersect).

Note that many games will also use a custom set of models & animations for this limb pass in the first-person view, allowing them to play with the positions, angles, and even scale of the limbs so it "looks right" from a first person perspective, even if it departs from what looks realistic from a third-person view.


In Unity, you can do this by...

  • setting aside a Layer for your first-person models. Assign those objects to this first-person layer.

  • set your main camera's Culling Mask to render everything except this first-person layer.

  • duplicate your camera, and make the clone a child of your original camera.

    • This child camera doesn't need any of the extra components like AudioListener, so you can remove those.

    • The child camera's culling mask should render only the first-person layer.

    • The child camera's near & far clipping planes should be set very close.

    • The child camera's Clear Flags should be set to Depth

    • The child camera's Depth should be set to 1 so it renders after the default camera.

\$\endgroup\$
  • \$\begingroup\$ oh wow, i am perhaps out of my depth ...again :]] . would you be any chance know of any small examples of doing this in Unity? Can Unity developers (ie. me, or you, or anyone else using it) actually manipulate the rendering process ?? In the old days I was using Libgdx or XNA and we had Render() or Draw() methods but in Unity I never seen those. \$\endgroup\$ – Big T Larrity Sep 11 '18 at 16:45
  • \$\begingroup\$ and that gif is funny on your link :DD helps me understand what you mean.. ( and tbh I kinda thought this might be the case but was maybe hoping it wasn't). So would a better solution be to use empty player capsule with just the gun for the player , and then each other player (ie. not local player in multiplayer, I would show the fully fledged 3d model version)? \$\endgroup\$ – Big T Larrity Sep 11 '18 at 16:48
  • \$\begingroup\$ Ahhh!! Wow I simply lowered the value in the 'near plane' of the camera and that has fixed it. Thanks for getting me on the right track again pal \$\endgroup\$ – Big T Larrity Sep 11 '18 at 16:57
  • 1
    \$\begingroup\$ I've edited the answer to include steps for Unity. Note that the further apart your near & far planes are (in terms of the ratio between them: how many multiples of near fit into far), the worse your depth precision can get. \$\endgroup\$ – DMGregory Sep 11 '18 at 17:10
  • \$\begingroup\$ @DMGregory I almost always grab the far clip plane (the default value 1000) and pull it in to 100 or so. At least until I have an idea of where it actually needs to be ("oh this'll be first person and my levels are 200 units big, I guess I need a further far clip plane then"). \$\endgroup\$ – Draco18s Sep 11 '18 at 18:00

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.