Search Unity

Player Follow Camera Goes Inside Objects

Discussion in 'General Graphics' started by siddharth3322, Nov 22, 2019.

  1. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    I was working on a 3d game where the camera is continuously following the player object in position and rotation fields.

    Complete 3d environment is set up as per my gameplay. A player moves and rotates in different areas of the environment, I was getting this kind of abnormal view in front of the screen. The camera goes into the other objects.

    player_follow_camera.png

    This is a really poor gameplay experience so how to fix this?
    How to make this look better when the player following the camera goes inside other environment objects?
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,627
    What's usually done is implement collision detection for the camera and have it compute and alternate position and angle on collision. This is tricky and even AAA games have problems with this (you may have noticed).

    Unity's Cinemachine package includes a pretty decent third-person follow camera with collision avoidance (much better than my humble attempt).

    https://docs.unity3d.com/Packages/com.unity.cinemachine@2.2/manual/index.html
     
  3. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    As @kdgalla mentioned, Cinemachine would probably be the most straightforward way to fix this (and get some nice camera controls as a bonus). If that doesn't work for you for some reason, a naive solution would be to cast a ray from the view target to the camera and push the camera in front of the hit point (if one exists). Obviously that could be improved with other techniques such as using "whisker" rays to pull the camera in in advance of a collision, but you should probably start with Cinemachine for that kinda thing before trying to roll your own (it's a lot of work to get feeling good).