# BGE: How do I setup camera collision?

I'm trying to make a game, and the camera goes beyond the walls of the room. Is there a way to stop the camera from doing that? Also, if it cannot be done without logic bricks only, can I please have a thorough explanation of the code used?

I'm basically trying to implement a system where if the camera collides with a wall, it moves a little forward.

I understand how this is a "noob" problem; but quick searches online either revealed methods that did not suit my needs, or methods that did not work.

Thanks.

This can be done with a very simple script. I've been using it out of a question asked earlier, but I can't find it anymore, so kudos for whoever made it. Essentially, this script uses a ray of variable length and moves the camera to the position that the ray hits at. Your going to need to add an empty that points toward the place you want the camera to be, and then put your mouse-look logic on the empty, not the player (at least for the up/down axis)

from bge import logic

def collision(cont):

own = cont.owner
scene = logic.getCurrentScene()
replace = scene.objects['ReplaceEmpty']
target = scene.objects['Camera.001']
ray = cont.sensors['Ray']

if ray.positive:
target.worldPosition = ray.hitPosition


also, (I'm not a code monkey) you'd probably need something on the end like

else:
target.worldPosition = replace.worldPosition


then, simple rig that up to a TLT always sensors, and Viola! cam-collision!

• Thanks! While this does look like the solution I'm looking for, could you please make a .blend file demonstrating the script, or point me to one? I'm having problems in setting up the script. – Droidge Aug 15 '15 at 8:11
• hokay, kinda busy today, but maybe later. – Scalia Aug 15 '15 at 14:08
• pasteall.org/blend/37664 – Scalia Aug 20 '15 at 15:17