Search Unity

Player Follow Camera Goes Inside Objects

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

  1. siddharth3322


    Nov 29, 2013
    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.


    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


    Mar 15, 2013
    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).
  3. GuitarBro


    Oct 9, 2014
    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).