How can I stop the Camera from detecting things behind walls.
I am working on a game and one of the mechanics is a security camera. I want it to work so that if you get seen by the camera when the timer goes off, you're out; however, as of now with my current code, even if you hide behind a wall, if you are still technically in the camera's FOV, you get "detected" and you lose the game. I want to know how I can get the same effect I have now, but without the camera being able to detect things. The only solution I can think of at the moment would be creating triggers in the areas that the camera technically couldn't see you and have the camera ignore the player if the player is in them, but that would require a total redesign of the 5 maps I already have completed. Any suggestions? P.S. I know the code is probably EXTREMELY unoptimized
The current code for the detection script is below:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class checkForPlayer : MonoBehaviour {
public GameObject player;
public GameObject loseScreen;
public GameObject skillCheck;
public AudioSource timerWarning;
public Text hideWarning;
public Transform target;
Camera cam;
public GameObject shoppingList;
public GameObject civillian;
public float timeRemaining;
public static bool ifLost;
public Rigidbody playerBody;
void Start()
{
cam = GetComponent<Camera>();
ifLost = false;
}
void Update()
{
if (timeRemaining > 0)
{
timeRemaining -= Time.deltaTime;
} else
{
CheckForPlayer();
hideWarning.text = "";
timeRemaining = 15;
}
if (timeRemaining < 5 && timeRemaining > 4.9)
{
if (!ifLost)
{
hideWarning.text = "HIDE";
if (!timerWarning.isPlaying)
{
timerWarning.Play();
}
}
}
}
public void CheckForPlayer()
{
Vector3 viewPos = cam.WorldToViewportPoint(target.position);
if (viewPos.z > 0 && viewPos.x > 0 && viewPos.x < 1 && viewPos.y > 0 && viewPos.y < 1)
{
player.GetComponent<Rigidbody>().velocity = (transform.forward * 0);
loseScreen.SetActive(true);
player.GetComponent<cartController>().enabled = false;
hideWarning.text = "";
skillCheck.SetActive(false);
ifLost = true;
shoppingList.SetActive(false);
civillian.GetComponent<Animator>().enabled = false;
civillian.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotationZ;
}
}
}
Your answer
Follow this Question
Related Questions
360 FOV camera. 1 Answer
When attaching my custom camera script camera shakes when player starts to move fast. 0 Answers
viewing on the Mouse Y axis not working. 0 Answers
How to zoom-in, in VR 0 Answers