Camera Reposition on Collide

Hi

Id like to know how people managed to reposition the camera when the camera collides with an object, for example to move the camera closer to the character when the character runs near the walls.

Please can anyone help!

Thanks

There are a number of implementations, but as far as the basics go, this is what I use:

  1. Keep a reference to the location that the camera SHOULD be at, if there are no collisions. Lets call it DesiredLocation.

  2. Raycast from the target player to where the camera should be (DesiredLocation).

  3. If the ray collides with an object, place the camera at Collision.hit.

  4. Otherwise, place the camera at DesiredLocation.

Make sure to use layer masks to only have the camera collide with objects you want it to, such as Terrain or buildings, and not players itself (Unless you want that). There are a few other things that can help perfect the process, such as casting to the corners of a bounding box rather than the camera itself. You may also want to Interpolate between the DesiredPosition and the actual camera placement for a smooth transition.

Hi thanks for the reply.
Im not quite sure what you mean as my camera is constantly following my character so there is no desired location … unless u mean the desired location which is of distance 5 to my character and height and so on… but the transforms is constantly changing.
Hope to hear from you