# How to trigger with camera?

I’m trying to make a scary game, and at one point a sound will go off behind the player. They will (In theory) turn around to investigate, and while their back is turned an object (a zombie or something) will appear behind them, giving them a scare when the turn back around. What code would I use to trigger a script or animation when the camera is pointed a certain direction? Do I use collision boxes? A piece of code? Magic? Help!

Hi
I am assuming it is First Person camera, so have a dummy object behind the player\camera and check when “Renderer.isVisible” or “Renderer.OnBecameVisible” becomes active. when it does, trigger your “zombie” spawn or anything you want.

Answering your question in bold: One easy way to trigger something when the camera is pointed in a certain direction would be to compare two vectors using Vector3.Angle(). Vector3.Angle() returns an unsigned angle between two vectors. So if the two vectors are pointed in a similar direction, then the angle between the vectors will be below some threshold. In the case of game objects like cameras and characters, you can use the ‘transform.forward’ to say where they are pointing. But having two vectors pointing in the same direction does not mean that one is behind the other. For example one may be beside the other. But if one vector is looking at the game object of the other vector, and if the angle is small, then the one is behind the other.

Here is a script that causes one game object to “circle around and sneak up” on another game object when that other game object has its back turned:

``````#pragma strict

var speed = 0.85;
private var player : GameObject;

function Start() {
player = GameObject.Find("Player");
}

function Update() {
transform.LookAt(player.transform);

if (Vector3.Angle(player.transform.forward, transform.forward) < 45.0) {
transform.Translate(transform.forward * speed * Time.deltaTime);
}
}
``````