Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Nooch · Aug 14, 2011 at 04:10 PM · triggersclassesfunctions

Creating re-usable action triggers

Hi guys,

I'm a unity & programming noob, just starting to wrap my head around certain coding concepts. The demo I'm working on has a variety of actions the player can do, by pushing a button while in proximity of an object. The flow might go like this:

Player walks in to proximity trigger > Action button is displayed > Player pushes button > Something happens

The "something happens" can be different things: opening a door, having a message window displayed, destroying an object. But they are all initiated in the same manner.

I understand how to set up the individual actions, but not how to make the code more reusable. For example, I can make a "door open" scipt that contains the trigger/button/action code. But if I then make a "display message window" script, it doesn't seem efficient to rewrite the same trigger/button code inside that as well. Or does it?? :O

This leads me to believe I should somehow bundle the first actions (trigger/button display/button push) in to a re-usable piece of code, that I could apply to any object. And then somehow change the "something happens" part, depending on what the object is.

So my question is, what's the best way to go about doing this?

1) Making the trigger/button code re-usable - so I can put it on any object I want to be interactive

2) Implementing the "Something happens" part? If I make a simple "open door" movement script, how can I activate this after my generic trigger action above?

Thanks for any advice! (I'm using javascript btw, if that makes any difference).


Here's an example of a script that I have on a trigger. It displays a button icon when you enter the trigger, then displays a message window once you push the button.

What I would like to do is take something like this, and be able to use it on different objects - so that I could trigger different messages, or different actions, once the player pushes the context action button.


 var showContextButton : boolean = false;
 var showQuest : boolean = false;
 var buttonImage : Texture;
 var buttonAudio : AudioClip;
 var buttonPushAudio : AudioClip;
 var gamePlayer : GameObject; //Player Object
 var gameCamera : GameObject; //Main Camera
 
 //when player enters trigger radius, play a sound and show context button
 function OnTriggerEnter (myTrigger : Collider) {
     if(myTrigger.gameObject.name == "Player"){
         AudioSource.PlayClipAtPoint(buttonAudio, transform.position);
         showContextButton = true;
     }
 }
 
 //stop showing context button when player leaves the trigger
 function OnTriggerExit (myTrigger : Collider) {
     if (myTrigger.gameObject.name == "Player") {showContextButton = false;}
 }
 
 //show context button or quest window
 function OnGUI () {
     if (showContextButton){
         GUI.DrawTexture(Rect((Screen.width / 2),(Screen.height * .75),64,64),buttonImage);
     }
     if (showQuest){
         FreezeOn();
         windowRect = Rect (10,10,300,150);
         windowRect = GUI.Window (0, windowRect, DoMyWindow, "Quest");
     }
 }
 
 //if player pushes context button, show quest window
 function OnTriggerStay (myTrigger : Collider) {
     if(Input.GetKeyDown(KeyCode.E)){
         AudioSource.PlayClipAtPoint(buttonPushAudio, transform.position);
         Debug.Log("Action!");
         showContextButton = false;
         showQuest = true;
     }
 }
 
 //contents of the quest window
 function DoMyWindow (windowID : int) {
     GUI.Label(Rect(10,25,280,100), "Your quest is to go to the desk!");
     if (GUI.Button (Rect (10,115,280,25), "Okay")){
         FreezeOff();
         showQuest = false;
         setPrefQuestDesk();
     }
     else{showQuest = true;}
 }
 
 //disable movement
 function FreezeOn(){
     gamePlayer.GetComponent(MouseLook).enabled = false;
     gamePlayer.GetComponent(CharacterMotor).enabled = false;
     gameCamera.GetComponent(MouseLook).enabled = false;
     Screen.showCursor = true;
 }
 
 //enable movement
 function FreezeOff(){
     gamePlayer.GetComponent(MouseLook).enabled = true;
     gamePlayer.GetComponent(CharacterMotor).enabled = true;
     gameCamera.GetComponent(MouseLook).enabled = true;
     Screen.showCursor = false;
 }
 
 //set quest to active
 function setPrefQuestDesk(){
     PlayerPrefs.SetString("QuestDesk", "active");

}

Comment
Add comment · Show 2
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 sacredgeometry · Aug 14, 2011 at 05:25 PM 0
Share

If you understand how to make the individual Items post the code you have for them and I will explain how to make them more reusable.

avatar image Nooch · Aug 14, 2011 at 09:27 PM 0
Share

Sure, thanks for the reply.

I've updated the post with a sample. Apologies in advance for how lame this code probably is! :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by darkxcalibur · Aug 14, 2011 at 05:11 PM

I think the best choice will be having a function on the object affected maybe called InUse() and make them have a tag of "Usable"

then in the player code may not work doing it by head.

OnTriggerStay(other : Collider) { if(other.tag == "Usable") { if (Input.GetButton("use")) { sendMessage(other, "InUse"); }

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Issues Inheriting classes and monoBehaviour 1 Answer

Getting only one collider form OnTriggerEnter2D or OnCollisionEnter2D. 0 Answers

calling buttons in triggers 1 Answer

The name '' does not exist in the current context 2 Answers

Get Object, Trigger Attached Method,Trigger method on collider script 1 Answer


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