- Home /
how to make the camera function automatically when the player comes near to it?
hii In my game i have been added some cameras to get extra effect,but i dont know how to make the camera function automatically when the player comes near to it. help me.... THANKING YOU.
Answer by aldonaletto · Feb 21, 2012 at 01:50 AM
The easiest way is to add a trigger volume to each camera, and use OnTriggerEnter to activate the camera and OnTriggerExit to deactivate it: click the camera and add a spherical collider to it (menu Component/Physics/Sphere Collider), then in the Inspector / Sphere Collider set Is Trigger and adjust the Radius to cover the volume you want. Finally, attach this script to the camera:
function Start(){ camera.enabled = false; }
function OnTriggerEnter(other: Collider){ if (other.CompareTag("Player")){ // remember to tag the player as "Player" camera.enabled = true; // enable camera } }
function OnTriggerExit(other: Collider){ if (other.CompareTag("Player")){ camera.enabled = false; // player out of range - disable camera } } If there are overlapping regions, more than one extra camera may be active at the same time; you may use each camera's Depth value to define a "priority": the camera with higher Depth will be rendered over the others (default Depth = 0).
I tryed it but its not working... is there any other option.
It should work! You must tag the player as "Player", and it must be a CharacterController or rigidbody.
Anyway, an alternative is to measure the player distance in update, activating the camera when it's lower than some limit - attach the script below to the cameras (remember to tag the player!):
var range: float = 20.0; // set the detection distance var target: Transform;
function Start(){ target = GameObject.FindWithTag("Player").transform; }
function Update(){ if (Vector3.Distance(target.position, transform.position)
Your answer
Follow this Question
Related Questions
switch between multiple cameras based on distance to target 2 Answers
How to use 2 Cameras in FPS 2 Answers
changing camera views 1 Answer
Run a simultaneous "viewer" build. 0 Answers