|
|
|
| Home Blender Games CG Projects University Projects Tutorials Resources Links Random Stuff |
3rd Person Camera Collision DetectionThis 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.
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.
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.
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.
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:
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. |