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.



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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.