Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Grizzly · Aug 30, 2014 at 11:19 AM · c#

Adding door sound line in the C sharp script help.

Hi every one I found this script on some assets that I found and its in Csharp ,I dont know C sharp :( ,can someone update this script just to play a sound when door opens and closes that I will put in inspector when the script updates? Thanks a lot whoever helps.

 using UnityEngine;
 using System.Collections;
 
 //Instruction:
 //Make an empty game object and call it "Door"
 //Rename your 3D door model to "Body"
 //Parent a "Body" object to "Door"
 //Make sure thet a "Door" object is in left down corner of "Body" object. The place where a Door Hinge need be
 //Add a box collider to "Door" object and make it much bigger then the "Body" model, mark it trigger
 //Assign this script to a "Door" game object that have box collider with trigger enabled
 //Press "e" (Default) to open and close the door
 //Make sure the main character is tagged "player"
 
 public class open_door : MonoBehaviour 
 {
     public float smooth = (float)2.0;
     public float DoorOpenAngle = (float)110.0;
     public float DoorCloseAngle = (float)0.0;
     public bool open = false;
     public bool enter = false;
     public string defined_key = "e";
     public AudioClip 
     
     // Update is called once per frame
     void Update () 
     {
         if(open == true)
         {
             var target = Quaternion.Euler (transform.localRotation.x, DoorOpenAngle, transform.localRotation.z);
             // Dampen towards the target rotation
             transform.localRotation = Quaternion.Slerp(transform.localRotation, target,
             Time.deltaTime * smooth);
         }
     
         if(open == false)
         {
             var target1 = Quaternion.Euler (transform.localRotation.x, DoorCloseAngle, transform.localRotation.z);
             // Dampen towards the target rotation
             transform.localRotation = Quaternion.Slerp(transform.localRotation, target1,
             Time.deltaTime * smooth);
         }
     
         if(enter == true)
         {
             if(Input.GetKeyDown(defined_key))
             {
                 open = !open;
             }
         }    
     }
 
     //Activate the Main function when player is near the door
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.tag == "Player") 
         {
             //Debug.Log("Trigger Enter");
             (enter) = true;
         }
     }
     
     //Deactivate the Main function when player is go away from door
     void OnTriggerExit (Collider other)
     {
         if (other.gameObject.tag == "Player") 
         {
             //Debug.Log("Trigger Exit");
             (enter) = false;
         }
     }
     //@youtube.com/user/maksimum654321
 }
 
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Grizzly · Aug 30, 2014 at 11:37 AM

Nevermind I fixed it myself ,literaly without looking anywhere .... tought it would be much harder then java ... sorry for dumb question ...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by MrMika · Aug 30, 2014 at 11:39 AM

First, you will need to add an Audio Source componement to the object that you are working with (in this case, the door that you want to open/close). You can do it like this:

  1. Select the object (the door)

  2. Click in Add Component > Audio > Audio Source

After that, you'll have to add some code to the script. In the line 22, add the name to the AudioClip, like this:

 public AudioClip doorSound;

And then add this line inside the if's in the OnTriggerEnter and OnTriggerExit functions:

 gameObject.audio.PlayOneShot(doorSound);

This will play the sound once, everytime you enter/exit the trigger zone of the door.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Grizzly · Aug 30, 2014 at 11:49 AM 0
Share

I just added audio source and in after line 47 I added audio.Play(); and added sound directly into audio source :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Get an Object to Automatically Move Forward 2 Answers

Get Boolean from another Script, C# 2 Answers

Do i have to use Thread function in unity? 1 Answer

How to make tab in editor near the top? 1 Answer

Error with vector3 initialization 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges