Blending Online

Home

Blender Games

Vertigo [Incomplete]
The Shooting Gallery
Shadowdale
Space Assault
Transcendent Christmas Demo

CG Projects

Lamp
Rod
Asgard Ship
Another Ship
James Pond
Sword of Nibelungos
Sword of Odin
Human Head

University Projects

Library Model Walkthrough
Flash Game
VRML World

Tutorials

Building a Menu
Camera Collision
Adding Music Support
Screen Effects
Static Shadows
Using Text
Walk Cycles

Resources

Additional Font Sets
Menu Builder Template
Custom Cursor Template

Links

Random Stuff

MMORPG Characters

3rd Person Camera Collision Detection

This tutorial will explain how to setup a 3rd person camera configuration which will detect collisions with walls and prevent the camera going through them. The camera will remain inside rooms and on collision with any objects will move closer to the player.

To begin with I'm assuming you already have a rigged character/vehicle/other object ready, which you intend the camera to follow:



Firstly we need to add a point on the character that the camera will always look at. This will usually be inside the head. If your character is stood with his back to a wall, the camera will move in very close to this point.

Add an empty and parent it to your character like so:



Now we need another point to represent the ideal position of the camera. If nothing blocks the path of the camera, this is where it will be positioned. For this tutorial I will be using a distance of 30 units, but you can adjust this later based on your requirements.

Place another empty 30 units behind the first one, and parent it to that one:



Now to setup the camera itself. If the camera collides with a wall, at certain angles it is sometimes possible to see through the wall. To prevent this from happening we want to place the camera slightly away from the wall in event of a collision.

To set this up, place another empty anywhere in your room and parent the camera to it like so:



The system is now setup and ready for programming. If you like you can rotate the 1st empty to position the camera in a more suitable place. However you'll need to wait until the logic setup is complete before you can see how that angle looks:



Setup your logic bricks according to this diagram.

Camera_Pivot is the first empty.
Camera_Position is the second empty.
Camera_Parent is the 3rd empty.

Notice that the Ray sensor has a range of 30. This is so it detects exactly upto the Camera_Position point. If you change the distance the 2nd empty is placed from your character, you will also need to change this value.

Also remember to ensure the Camera_Parent empty is tracking in 3D.

Use this text for the Camera Parent script:

g = GameLogic
c = g.getCurrentController()
o = c.getOwner()

Ray = c.getSensor("Collision")

if Ray.isPositive():

    Collision = Ray.getHitPosition()

    Camera = c.getSensor("CamPar").getOwner()
    Camera.setPosition(Collision)

else:

    OrigCamera = c.getSensor("CamPos").getOwner()
    OrigCameraLoc = OrigCamera.getPosition()

    Camera = c.getSensor("CamPar").getOwner()
    Camera.setPosition(OrigCameraLoc)


That's it! If you now move your character around, the camera should follow and move closer if it collides with any walls or something obstructs the view of the character.

Here's a demo file if you want to see a working example.