Search Unity

Triggers help

Discussion in 'Editor & General Support' started by tom4396, Nov 18, 2019.

  1. tom4396


    Nov 2, 2019
    I am trying to make a script where when the player opens the door there is a blank brick wall behind it with blood writing on (it’s a horror game) and it plays a sound , could someone help me out on a script or on how to do this ? I have very little knowledge of scripting and have been using unity for a few weeks now and just can’t take it all in .
  2. dgoyette


    Jul 1, 2016
    Well, I doubt you're going to find a script that opens a door, revealing a brick wall with bloody writing on it. That behavior is a combination of several unrelated things.

    First of all, the visual don't have anything to do with a script, in general. In most cases, you'd model the environment using ProBuilder, or an external tool like Blender, to create the 3D geometry, and then use some other program to texture it. All all of the 3D stuff you'd build that outside of a script.

    The "script" side of things comes in when you want to make something move in your game. You can add a collider set to "Is Trigger", then add a script to it which contains an "OnTriggerEnter()" method. In that script, you can animate the movement of a door, set a trigger on an animation controller, etc, to get a door to open, among other things.

    Ultimately, what you're asking for sounds pretty simple, but it touches on a fair number of topics. How far are you along on the 3D visuals? Do you have the room/house/door created yet? Do you have a first-person character who can walk around in that space?
  3. SisusCo


    Jan 29, 2019
    You can utilize the Hinge Joint to make a door that can be opened. Then you can use "Target Position" under the "Spring property to make the door open to the given angle.

    For the sound you need the AudioSource component. Set "Play On Awake" to False and manually call
    AudioSource.Play() when you want to play the sound.

    You'll also need to figure out what player input would cause the door to open, and call the Open method when you detect that. One way to do it is using OnMouseDown.

    Here's something to help you get started:

    Code (CSharp):
    1. using UnityEngine;
    3. [RequireComponent(typeof(BoxCollider))]
    4. public class Door : MonoBehaviour
    5. {
    6.     // set  angle for door when it is open
    7.     public float openAngle = 120f;
    9.     // assign this using the inspector
    10.     public HingeJoint hinge;
    12.     // assign this using the inspector
    13.     public AudioSource audioSource;
    15.     public bool isOpen = false;
    17.     void OnMouseDown()
    18.     {
    19.         if(!isOpen)
    20.         {
    21.             Open();
    22.         }
    23.     }
    25.     void Open()
    26.     {
    27.         isOpen = true;
    28.         var hingeSpring = hinge.spring;
    29.         hingeSpring.spring = 5;
    30.         hingeSpring.damper = 3;
    31.         hingeSpring.targetPosition = openAngle;
    32.         hinge.spring = hingeSpring;
    33.         hinge.useSpring = true;
    35.         audioSource.Play();
    36.     }
    37. }
  4. tom4396


    Nov 2, 2019
    I should have clarified a little , this is the scene i have so far, i can open and close my door and it already has a sound effects for opening and another for closing, i need a script that either:
    1. Plays a sound when the player looks at the brick wall (don't know if this is possible)
    2.Add an additional sound effect to the door opening as well as a door open sound (this wouldn't look great as the player can open from an angle blocking their vision of the brick wall so they have no idea why the sound played
  5. tom4396


    Nov 2, 2019
  6. dgoyette


    Jul 1, 2016
    Cool. Both of those are pretty easy to add to what you've got.

    For the first one, you should look at the Physics.Raycast() method. The basic idea with Raycast is it draws an invisible line for A to B, and checks whether it hit a certain object on the way. You'd draw a line "forward" from the camera, and see if it hits the door. If it does, you'll know the player is looking at the door. (Note that if you want this to be a bit less precise, you can use Physics.Spherecast instead, but the general idea is exactly the same.)

    Within that script that does the raycasting, you'd probably want to store a "time" value of the last time you played the sound, so that you don't play it repeatedly as the player looks at it. What follows is some untested code to give you the general idea, but you'd need to clean it up and provide the proper values:

    Code (CSharp):
    1. public AudioSource SwingingDoorAudioSource;
    3. float _lastSwingingingDoorSoundPlayed;
    5. void Update() {
    6.     RaycastHit hit = null;
    7.     if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward * 15, out hit)) {
    8.         // There are various ways to test for whether you hit the door or not. Here are three different
    9.         // common ways to know whether the thing you hit is the Door. Pick one of the following three ways
    10.         // to test whether the door was hit:
    12.         bool doorWasHit = false;
    13.         if (hit.collider.gameObject.CompareTag("SwingingingDoor")) {
    14.             // Assumes you placed the tag "SwingingingDoor" on the object.
    15.             doorWasHit = true;
    16.         }
    18.         if (hit.collider == SwingingingDoorCollider) {
    19.             // Assumes SwingingingDoorCollider has been set on a property on this class.
    20.             doorWasHit = true;
    21.         }
    24.         if (hit.collider.gameObject.GetComponent<SwingingingDoorController>()) {
    25.             // Assumes your swinging door has a "SwingingingDoorController" component on it.
    26.             doorWasHit = true;
    27.         }
    29.         // Now play a sound if we didn't play one recently.
    30.         if (doorWasHit && Time.timeSinceLevelLoad - _lastSwingingingDoorSoundPlayed > 5) {
    31.             // Play a sound if we were hit, and we haven't played the swinging door sound in at least 5 seconds.
    32.             _lastSwingingingDoorSoundPlayed = Time.timeSinceLevelLoad;  
    33.             SwingingDoorAudioSource.Play();
    34.         }
    35.     }
    36. }

    As for the second thing, it sounds like you want to play a scary sound, in addition to the door opening sound, but only if the player is standing in front of the door. One approach you can take is to use Physics.OverlapBox to test whether the player's collider is within a certain volume. So, as you open the door, you can check whether the player is within an invisible box a few meters around the box. OverlapBox ( has some stuff in common with Raycast, so it should be reasonably simple to get that working.

    Anyway, hopefully that helps.
    SisusCo likes this.
  7. tom4396


    Nov 2, 2019
    I just tried adding a graphic ray caster to my wall and all the walls in my scene just disapeared due to them all being prefabs
  8. dgoyette


    Jul 1, 2016
    That's not the same thing. Graphic Raycaster is something you'd use for 2D stuff, in a UI Canvas.

    You want Physics.Raycast (