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.

Here's the .blend: https://drive.google.com/file/d/0B1cRCpiEAXrtOGxWYmRNS3c5RTg/view?usp=sharing

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.



1 Answer 1


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

    target.worldPosition = replace.worldPosition

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

  • $\begingroup$ 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. $\endgroup$
    – Droidge
    Commented Aug 15, 2015 at 8:11
  • $\begingroup$ hokay, kinda busy today, but maybe later. $\endgroup$
    – ruckus
    Commented Aug 15, 2015 at 14:08
  • $\begingroup$ pasteall.org/blend/37664 $\endgroup$
    – ruckus
    Commented Aug 20, 2015 at 15:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .