Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Dealing with FOV differences when camera stacking in an FPS

Discussion in 'General Discussion' started by Weaver, Mar 20, 2021.

  1. Weaver

    Weaver

    Joined:
    Jul 2, 2013
    Posts:
    18
    Hi all, I'm prototyping a quick FPS and I'll admit I have no development experience with any first person genres. I'm sure this issue is a common one that there is a set of best practices to overcome it but I can't figure out how so I was hoping for some guidance.

    I've got a traditional camera stack with two cameras where the weapon camera is rendered above the main camera. The weapon camera has an FOV of around 40 to avoid any severe model stretching of the weapon. The main camera has a user selectable FOV. In the screenshot it's around 80. Both cameras maintain the exact same transform aside from their FOVs.

    Anyways, I want to make some weapons shoot real rigidbody projectiles instead of relying on only hitscan bullets. The issue comes making the projectiles appear at (or even near) the muzzle in the main camera when the FOV's are different as the larger the FOV difference between the weapon and main camera are, the further the spawn point of the projectiles appears to move away from the gun muzzle. In the image the red ball is the emission point of the projectiles and as you can see in the editor is it fixed to the front of my poorly modeled gun (I'm a programmer not an artist lol).

    I was thinking about doing something like making the emission point being always behind the weapon at the position of the cameras because the 0 distance should appear to always be in the same spot for both cameras. Another option I was thinking was to try to actually calculate a projection difference between the two cameras and find the visual point that way.

    Any advice, tips, or tricks on how to overcome this is greatly appreciated.
     

    Attached Files:

  2. Billy4184

    Billy4184

    Joined:
    Jul 7, 2014
    Posts:
    5,984
    1. Use https://docs.unity3d.com/ScriptReference/Camera.WorldToViewportPoint.html to get the viewport coordinates of the emission point using the high-FOV game camera.
    2. Use Camera.ViewportToWorldPoint with the low-FOV weapon camera to get the world position that would look like it was in the same place.
    3. Position your gun relative to this point so that the barrel exit is positioned at this point.
     
  3. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,325
    Also I wouldn't bother with a different weapon fov on a different camera in a modern game. Mostly because if you do that, then you'll have difficulty receiving shadows on the weapon and the like.
     
  4. Weaver

    Weaver

    Joined:
    Jul 2, 2013
    Posts:
    18
    Thanks. I did almost the reverse of what you suggested and instead moved the emission point with an inverse of the projections you suggested. The result is pretty acceptable with minimal triangulation issues at the reticule. Detectable when a player might try to soot the corer of a small object at very close distances but not very noticable otherwise. I only plan to use projectiles for things like grenades and rockets anyways so pinpoint accuracy is not needed for these instances.


    This is a good point. I've modified my weapon to sit on the same camera to receive dynamic shadows and used a modified shader as one the Unity shooter project used (that is now getting pretty old) to flatten the perspective using a material. I've seen a similar approach taken in more modern FPS games along with using panini projections.

    The only downside to this approach is you can get model intersections with the environment if you aren't careful. To try to mitigate this I've made the weapons very small as so they should (hopefully) always reside within the player's collision capsule to avoid environmental geometry intersections.