- Home /
Stop camera from going through walls
I am using the third person controller standard asset from Unity as my playable character. My scene layout looks like a small office, so there are a lot of rooms and a lot of walls. When I run around with my character and I am standing with it's back against the wall, the camera will show the other side of the wall. I tried doing a couple of other solutions I have read on here but none of these seem to work.
The ones i tried are:
putting near clipping planes to 0.01
http://wiki.unity3d.com/index.php/SmoothFollowWithCameraBumper (attached script to main camera and the 3rd person controller as target)
The second solution (the smoothfollowwithcamerabumper script) is doing what I want (I do have to uncheck the ThirdPersonCamera script component from the 3rd person controller), however turning around in the game is unbearable. one small click on the left arrow will make the character (with camera) INSTANTLY turn around 90° while it used to delay and smoothen out the camera, and the movement is just not right and playable at all. Also rotating 180° by clicking the up/down arrow is just not possible and will make the camera go all wild for some time.
I tried playing a bit with the parameters in the smooth script but this was no solution at all.
Does anyone know why this is happening? Thank you in advance.
Does your camera have a collider attached to it so it can actually collide with things?
You can also raycast from the camera and make sure the thing you hit is the player. If not the player then have a set of camera points to try until you get one that works. Handy for indoor/caves, well I guess caves are indoor as well!
You can see an example in the S$$anonymous$$lth Tutorial Game: S$$anonymous$$lth Web Player
Run behind a building and the camera swings to a more top down location.
Can't work out the tutorial file locations as the ones that mention the Camera appear to be from a different tutorial but YouTube has plenty of examples and you can still download the asset and view the scripts directly.
@SuperReaper1999 No it does not. However I tried to add a box collider component to the '$$anonymous$$ain Camera' but this does not change anything at all.
@$$anonymous$$mmpies thank you for the idea, but I think that might get a bit too complicated. I will look into it if really nothing else can solve my problem. Thank you!
The second solution was ok for me. Sometimes is not so reliable but after a lot of searching this is the easy one and very straightforward. Thanks for sharing.
Answer by drudiverse · Dec 11, 2014 at 08:30 PM
put a small invisible collider around the camera so that it doesnt go into any other colliders.
I tried doing this but I dont really have any idea how to do it properly, since the way I am trying it, it does not work. Do I just in the inspector select my '$$anonymous$$ain Camera' and then add a component to this? (Physics -> box collider).
I tried this, and it does not work at all.
Answer by thegdeveloper · Feb 16, 2016 at 06:01 PM
I had the same isue and at first I had a character controller onto the camera and I made the traditions of the camera with the Move() method but this has problems when there is an object between the camera and the playr because the camera can stuck behind. So I have removed the controller and I make a recast from the player to the camera. If it hits something then i move the camera forward of the position that there is a collision. Another approach is to keep the character controller and make a ray cast from the camera towards the player. If it hits something use disable the collider and the camera does not stuck but sometimes you see thing through the mess. So I have chosen the approach that I mention earlier. play with whatever solution does the job for you