Search Unity

Request : camera alarm

Discussion in 'Scripting' started by Anazoth, Aug 19, 2012.

  1. Anazoth

    Anazoth

    Joined:
    Apr 16, 2012
    Posts:
    38
    Hey, i am pretty new to coding in general and i am currently trying to learn (i usually stick to modelling).

    I am looking for help on how to make an alarm censor. I am looking to put a rotating camera model on a wall which shoots out a red light as it rotates. If the character steps on this light, i want an alarm to sound. How would i go about doing this :confused:
     
  2. Vulegend

    Vulegend

    Joined:
    Jul 15, 2012
    Posts:
    84
    This is what i used for when the bullet hits my target , so maybe it could help. It requires you to have a collider that is flagged as a trigger around your character , if you already have the collider on it , just child an empty game object to it with a trigger collider. I think there is a better and more efficient way but i like it like this :p . Also your instantiated prefab (the laser) should be tagged as "Laser". Also make sure that the script is attached to the object with Trigger collider

    Code (csharp):
    1.  
    2. public class Example: MonoBehaviour {
    3.  
    4. void OnTriggerEnter ( Collider other )
    5.     {
    6.         if(other.gameObject.CompareTag("Laser"))  // If the character is hit by the "Laser"
    7.         {
    8.             // Do stuff
    9.         }
    10. }
    11.     }
    And if you want it to only do a sound effect you can go with

    Code (csharp):
    1.  
    2. public class Example: MonoBehaviour {
    3.  
    4. public AudioClip Alarm;
    5.  
    6. void OnTriggerEnter ( Collider other )
    7.     {
    8.         if(other.gameObject.CompareTag("Laser"))  // If the character is hit by the "Laser"
    9.         {
    10.             AudioSource.PlayClipAtPoint(Alarm, transform.position);  // Play the Alarm sound on the location of the character
    11.         }
    12. }
    13.     }
    If you want the sound shot to be played at the position of camera , make an public GameObject cameraObject , assign the camera game object to it in inspector (or go with GameObject.Find) and replace the "transform.position" with cameraObject.transform.position
     
    Last edited: Aug 20, 2012
unityunity