- Home /
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!
Ima go with magic too on this one.
A famous person once said something similar to this: "$$anonymous$$agic is just the science that is beyond us"
Take that however you like ;)
Answer by maddymac · Jul 06, 2013 at 06:57 AM
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.
Answer by robertbu · Jul 06, 2013 at 07:13 AM
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);
}
}
Your answer
Follow this Question
Related Questions
Move character in direction its facing 1 Answer
Need help with third-person camera stuttering involving raycasts and clamp 0 Answers
Can't get AR camera to trigger events 0 Answers
Not shooting in desired direction 1 Answer
Getting 3rd person to look in direction of camera when right mouse clicked 1 Answer