- Home /
RPG Camera with walls
Hi, everybody. I do game for mobile devices. I have a character, a maze and the camera (3rd person camera). The camera follows the character, but doesn't rotate. There are situations when the character is behind a wall of a maze and it it isn't visible. Whether there is any correct method of implementation of the camera (without approaching it to the character) that it was possible to see the character always? Maybe it is possible to show somehow a contour of the character if it stands behind a wall? Or can it is necessary do something other? Thanks in advance for the help.
Answer by 03gramat · May 17, 2014 at 10:17 AM
If you have the walls of the maze split up into sections, you can use a raycast to fire from the camera to the player and if it hits a wall (tagged as wall), you can set that section of wall to be invisible.
///Add this script to camera (JavaScript)
#pragma strict
var target : Transform; // Your player
var hiddenWall : Transform; // The last wall we hid
function FixedUpdate(){ //physics is best done on fixedupdate
var rayHit : RaycastHit; // what our ray hit
if(Physics.Raycast(transform.position, target.position - transform.position, rayHit)){ //shoot a ray from camera to player
if(rayHit.collider.transform.tag == "wall"){ // if the ray hit a wall
if(hiddenWall == null || hiddenWall == rayHit.collider.transform){ // if we are hitting a wall after not OR if the wall is the same as the last one we hit
hiddenWall = rayHit.collider.transform; // set our hiddenwall to the one we hit
hiddenWall.renderer.enabled = false; // hide that wall
}
else{ // otherwise (if we have hit a wall that isn't the same as the last wall we hit)
hiddenWall.renderer.enabled = true; // make the last wall visible
hiddenWall = rayHit.collider.transform; // // set our hiddenwall to the one we hit
hiddenWall.renderer.enabled = false; // hide that wall
}
}
else{ // we arent hitting a wall.. so...
if(hiddenWall){ // if we just stopped hitting a wall
hiddenWall.renderer.enabled = true; // make the last wall visible
hiddenWall = null; // we aren't hitting a wall anymore
}
}
}
}
I hope that makes sense, and more importantly works and works how you want it to!(It's untested)
@03gramat Visual deleting a wall (wall=null) will affect negatively the general structure of a maze. Then it is better not to delete a wall, and to do it to the transparent. But me interests: whether it is possible to draw a contour of the character who is behind a wall?
Your answer
![](https://koobas.hobune.stream/wayback/20220613145230im_/https://answers.unity.com/themes/thub/images/avi.jpg)