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.
Create a solid object that represents your character, and parent the camera to that.
If you’re going for an FPS system, you might want to have two objects: one for the body and one for the head. The body rotates with x side-to-side motion, and the head rotates with y up-and-down motion. In this case, parent the camera to the head, and parent the head to the body.
Thanks, Guramarx. That is exactly what I was looking for, sorry for not searching for it before making a thread.
However, there is an issue. The camera works perfectly in the .blend by Mobious, but when I import the camera along with the scripts to a scene, it doesn’t work properly. Everything, including the mouse zooming and movement works fine, but the collision (which is what I’ve been trying to implement as well) just doesn’t. I also tried importing other cameras; everything worked, but the collision did not. I then tried copying the script’s code into my project. As mentioned above, everything but the collision worked. What is going on here?
I think the ray sensors only pick up collision with object that contain the “block_camera” property, you’ll need to add this property to your environment on your own, after that you can copy this property from one object to all other objects, press A to select everything and remember to SHIFT+RightCLICK the object with the property last, then press SPACE and then enter “Copy Game Property”
That is exactly what I was looking for, sorry for not searching for it before making a thread.
You didn’t do anything wrong, feel free to ask anything, its why this forum exist
Thanks! That seemed to work. Now, there is one thing left to do.
I want only the camera collision to work from Mobious’ script. While the camera movement and zooming-in and smoothing is great, I’d like to use my custom system.
Is there a way to selectively remove parts from the script and only make the collision thing work? Editing the script makes it fail completely for me.
I’m not too sure if you can remove the “movement” because I think* that is what had “taught” the camera to avoid collision in the first place
If what you want are the ray sensors to pick up collision then copy the logic bricks to your camera however, your camera will not avoid collision with other objects
import bge
def main():
cont = bge.logic.getCurrentController()
own = cont.owner
CTRL = cont.sensors['CTRL']
if 'ZHome' not in own:
for objects in bge.logic.getCurrentScene().objects:
if 'ZoomHome' in objects:
own['ZHome']=objects
print('FoundZoom')
if 'CameraHome' in objects:
print('FoundHome')
own['CamHome']=objects
if 'PlayerProp' in objects:
own['Player']=objects
print('FoundPlayer')
else:
own.applyForce((0,0,9.8*own.mass),0)
Ray = own.rayCast(own['Player'].worldPosition,own.worldPosition,0,'',0,0,0)
bge.render.drawLine(own['Player'].worldPosition,own.worldPosition,(1,0,0))
if Ray[0]:
print(Ray[0])
if Ray[0]!=own['Player']:
if own['Zoom']<=9:
own['Zoom']+=1
else:
if own['Zoom']>=1:
own['Zoom']-=1
else:
if own['Zoom']>=1:
own['Zoom']-=1
V=[]
if not CTRL.positive and own['Zoom']<10:
if Ray[0] or Ray[0]==Player:
V=[]
if Ray[0]:
if Ray[0]!=own['Player']:
own.worldPosition=Ray[1]+(own.worldOrientation.col[2]*-.1)
own.worldLinearVelocity*=0
else:
V = own.getVectTo(own['CamHome'])
else:
V = own.getVectTo(own['CamHome'])
own.alignAxisToVect(own['CamHome'].worldOrientation.col[2],2,.1)
own.alignAxisToVect(own['CamHome'].worldOrientation.col[1],1,.1)
if CTRL.positive or own['Zoom']==10:
##print('Zoom2')
V = own.getVectTo(own['ZHome'])
own.alignAxisToVect(own['ZHome'].worldOrientation.col[2],2,.1)
own.alignAxisToVect(own['ZHome'].worldOrientation.col[1],1,.1)
own.applyForce(V[1]*V[0]*(300-(own['Zoom']*30)),0)
own.worldLinearVelocity*=.00125
main()
Gravity Camera
left ctrl = Zoom in
Space = place obstruction in front of camera
It uses magic, and golden unicorns.
(actually it uses physics and hax)
Hax= Camera is slow parented so you don’t viberate until you explode
or go into some sort of superposition
import bge
def main():
cont = bge.logic.getCurrentController()
own = cont.owner
CTRL = cont.sensors['CTRL']
if 'ZHome' not in own:
for objects in bge.logic.getCurrentScene().objects:
if 'ZoomHome' in objects:
own['ZHome']=objects
print('FoundZoom')
if 'CameraHome' in objects:
print('FoundHome')
own['CamHome']=objects
if 'PlayerProp' in objects:
own['Player']=objects
print('FoundPlayer')
else:
own.applyForce((0,0,9.8*own.mass),0)
Ray = own.rayCast(own['Player'].worldPosition,own.worldPosition,0,'',0,0,0)
bge.render.drawLine(own['Player'].worldPosition,own.worldPosition,(1,0,0))
if Ray[0]:
##print(Ray[0])
if Ray[0]!=own['Player']:
if own['Zoom']<=9:
own['Zoom']+=1
else:
if own['Zoom']>=1:
own['Zoom']-=1
else:
if own['Zoom']>=1:
own['Zoom']-=1
V=[]
if not CTRL.positive and own['Zoom']<10:
if Ray[0] or Ray[0]==Player:
V=[]
if Ray[0]:
if Ray[0]!=own['Player']:
own.worldPosition=Ray[1]+(own.worldOrientation.col[2]*-.1)
own.worldLinearVelocity*=0
else:
V = own.getVectTo(own['CamHome'])
else:
V = own.getVectTo(own['CamHome'])
own.alignAxisToVect(own['CamHome'].worldOrientation.col[2],2,.1)
own.alignAxisToVect(own['CamHome'].worldOrientation.col[1],1,.1)
if CTRL.positive or own['Zoom']==10:
##print('Zoom2')
V = own.getVectTo(own['ZHome'])
own.alignAxisToVect(own['ZHome'].worldOrientation.col[2],2,.1)
own.alignAxisToVect(own['ZHome'].worldOrientation.col[1],1,.1)
if len(V)>0:
own.applyForce(V[1]*V[0]*(300-(own['Zoom']*30)),0)
own.worldLinearVelocity*=.00125
main()
Hello, Radish! Nice to see you here.
Thanks for the .blend. Looks like your way of setting up camera collision with walls is good enough for my purpose.
However, being the noob that I am, I still can’t get it to work. So I decided to upload a newer version of the .blend, with all I could do in setting up the script. If you ever get the time, please do consider looking at it and telling me what I’m doing wrong with the code.