- Home /
Preventing camera clipping
Hello, I wondering if anyone found a good way to prevent camera from going through walls in tight areas? I went through tens of tutorials and threads but the most common method seems to be casting ray to see if camera is obstructed by something, the problem with this is every tree or small object that will obstruct the camera makes it jump to the player.
I recorded this video https://www.youtube.com/watch?v=5E2DTWJYERU From this asset demo https://www.assetstore.unity3d.com/en/#!/content/11716 Which is basically Buzz3d 3rd person tutorial
anyway you can see how camera jumps every time tree gets in the way. Now one thing I could do is introduce layers for raycast to ignore the trees etc, but that is kind of a pain in the long term for environment artists.
One other method I found was from this tutorial series https://www.youtube.com/watch?v=b0PvJ4AWvWQ&list=PLKFvhfT4QOqlEReJ2lSZJk_APVq5sxZ-x where he kind of checks vector intersection point between camera and player and move camera on X and Z to that vector. But he still have collision issues in the corners.
I stumbled upon Unreal Engine tutorials and they had this spring physics system, where camera had collision and was spring attached to player and if camera collided with something spring would contract. Would this work for unity? Have any of you guys tried this?
Or if you have any solutions that works well work 3rd person camera that would be awesome!
Thanks in advance
Answer by MadMenyo · Jan 19, 2015 at 01:39 AM
You could add tags to everything that should jump the camera, or perhaps if easier add tags to the things that should not jump the camera.
Another thing is to just keep the camera really close when in tight space. If you are bothered with the visibility of your player you can try lowering the opacity on the materials of the meshes between the camera and the player.
Answer by AgapitoTori · Mar 27, 2016 at 08:13 PM
@sotha_sil A way to avoid the jumping is to make the camera move slowly only on the way out. This means that when the raycast hits an obstacle the camera moves immediately forward (when it collides with the tree), but when the raycast stops detecting an obstacle, instead of instantly returning to the max distance the camera is set to, you slowly interpolate it, this kinds of smoothes the camera movements, especially in enclosed spaces.
Also, in order to avoid wall clipping when the character is too close to walls consider using spherecast instead of raycast, a spherecast with a big enough radius will make it impossible for the nearClipPlane of the camera to "half traverse" a wall.
Finally I would strongly recommend to thourougly check the protectFromWallClipping script that comes with the standard assets (or just use it), I haven't found any camera tutorials better than this about the subject (for my coding level at least).
Your answer
Follow this Question
Related Questions
MouseOrbitImproved camera distance seemingly inverted 1 Answer
Camera looking through objects when touching 1 Answer
Camera Question 0 Answers
Stop Dashing through walls 1 Answer