• 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 wokzop · Jun 28, 2018 at 05:51 PM · destroy objectkeysdoors

how i can make a gate destroy when the door open ?

Hello, I have finish My door and key script , (when i get a key i can open a door ) But i want the gate that is on the door, to Destroy when the door open And also how i can put a sound of the door when he open and close . Here is the scripts if you can help me, by edit the scripts or give me a script to do that , or tell me how to do that ... Many Thanks for your help .

The Scripts : door: using UnityEngine; using System.Collections;

public class DoorScript : MonoBehaviour {

 public static bool doorKey;
 public bool open;
 public bool close;
 public bool inTrigger;

 void OnTriggerEnter(Collider other)
 {
     inTrigger = true;
 }

 void OnTriggerExit(Collider other)
 {
     inTrigger = false;
 }

 void Update()
 {
     if (inTrigger)
     {
         if (close)
         {
             if (doorKey)
             {
                 if (Input.GetKeyDown(KeyCode.E))
                 {
                     open = true;
                     close = false;
                 }
             }
         }
         else
         {
             if (Input.GetKeyDown(KeyCode.E))
             {
                 close = true;
                 open = false;
             }
         }
     }

     if (open)
     {
         var newRot = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0.0f, -90.0f, 0.0f), Time.deltaTime * 200);
         transform.rotation = newRot;
     }
     else
     {
         var newRot = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0.0f, 0.0f, 0.0f), Time.deltaTime * 200);
         transform.rotation = newRot;
     }
 }

 void OnGUI()
 {
     if (inTrigger)
     {
         if (open)
         {
             GUI.Box(new Rect(0, 0, 200, 25), "Press E to close");
         }
         else
         {
             if (doorKey)
             {
                 GUI.Box(new Rect(0, 0, 200, 25), "Press E to open");
             }
             else
             {
                 GUI.Box(new Rect(0, 0, 200, 25), "Need a key!");
             }
         }
     }
 }

}

Key: using UnityEngine; using System.Collections;

public class DoorKey : MonoBehaviour {

 public bool inTrigger;

 void OnTriggerEnter(Collider other)
 {
     inTrigger = true;
 }

 void OnTriggerExit(Collider other)
 {
     inTrigger = false;
 }

 void Update()
 {
     if (inTrigger)
     {
         if (Input.GetKeyDown(KeyCode.E))
         {
             DoorScript.doorKey = true;
             Destroy(this.gameObject);
         }
     }
 }

 void OnGUI()
 {
     if (inTrigger)
     {
         GUI.Box(new Rect(0, 60, 200, 25), "Press E to take key");
     }
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hectorux · Jun 28, 2018 at 10:55 PM

With the sound you can atach a AudioSource on the door and when you press e, tell it to play, also instead of using a bool called isTrigger, you could use OnTriggerStay() and put there your code instead on the Update. To destroy the gate when is fully open, first put the max rotation in an "if" and then there tell it to destroy

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

86 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 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 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 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

Key script not working right! 1 Answer

looking for help with item pickups 1 Answer

[Duplicated Question] Opening multiple doors 0 Answers

open a animated door 3 Answers

I need to make a door open only when the player's essence has reached a certain level 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges