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 AW0610AUT · Dec 15, 2013 at 01:55 PM · animationguiinterface

Airlock Door Code Problems [Solved]

 var anim : String;
 var AnimationSource = gameObject;
 
 function OnTriggerStay()
 
  { 
 
 if(Input.GetKeyDown(KeyCode.E))
 animation.Play("AirlockOpen", PlayMode.StopAll);
 }

Ok so this is my code. It works so far. Now i want 2 things.


1.) When my player enters the trigger zone i want to display a message (or even better a image) somewhere in the bottom center of the screen. Something like "Open/Close [E]". A picture would be better because i can stylize it more than a font.


2.) Somehow i have to check if the door is open. Maybe by setting a var when i play the animation to open and by setting it again when i play the animation to close. So my script know when to close by pressing "E" and when to open. Right now i have only one animation that opens the door. Do i need one to close it aswell or can i just play the "Open-animation" backwards?


I know i already had a topic for that code but for a different problem so i wasnt sure if i should post a new Q or use the old one.

Anyway, help is much appreciated, thanks.

PS: I'm from Austria so my English isn't the best.

Ok i solved it. This is the working code for anyone who needs it. I hope i explained everything in the code. Its JavaScript btw. Thanks to everyone who helped me.


 var AnimationSource = gameObject;//Source of the animations used in the Script
 var inside : boolean = false;//var that stores inside or outside of trigger // Used for Open/Close Hint
 var image : Texture2D;//Texture of Open/Close Hint
 var open : boolean = false;//var that stores Open/Close state of door
 //----------------------------------------------------------------------
 function OnTriggerEnter (col : Collider) //Displaying the Open/Close Hint
     {
 
         if(col.gameObject.name == "First Person Controller")//PlayerName
         {inside = true;}
     }
 //---------------------------------------------------------------------
 function OnTriggerStay (col : Collider) 
 {
 
     if(col.gameObject.name == "First Person Controller")//PlayerName
 
         {
             if         (Input.GetKeyDown(KeyCode.E)&& open == false && !animation.IsPlaying("AirlockClose"))//Closing the Airlock //Making sure that no other animation is playing
                     {
                     open = true;
                     animation.Play("AirlockOpen", PlayMode.StopAll);
                     }
                 
         else if        (Input.GetKeyDown(KeyCode.E)&& open == true && !animation.IsPlaying("AirlockOpen"))//Closing the Airlock //Making sure that no other animation is playing
                     {
                     open = false;
                     animation.Play("AirlockClose", PlayMode.StopAll);
                     }
         }
 }
 //-------------------------------------------------------------------------
 
     function OnTriggerExit (col : Collider) //removing Open/Close Hint
         {
 
             if(col.gameObject.name == "First Person Controller")//PlayerName
             {inside = false;}
         }
 //-------------------------------------------------------------------------
     
  
 function OnGUI()//hint for opening/closing 
     {
         if (inside == true)
             {
             GUI.Label(Rect(Screen.width/2-128,Screen.height-Screen.height/3,256,64), image);
             }
     }
 
 
     
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
1
Best Answer

Answer by slayer29179 · Dec 15, 2013 at 02:32 PM

Hello my friend! 1: You could use a Boolean variable when you stay inside the collider and turn it on and off. When its on you will use a GUI Box to show your image. For example:

 var inside : boolean = false;

 var image : Texture2D;
 
 function OnTriggerStay (col : Collider) //Using col and collider you can make it work ONLY for the door and not for any other triggers. Col will store the object hit.
 {
 if(col.gameObject.name == "NAME_OF_COLLIDER")
 {
 inside = true;
 
 if (Input.GetKeyDown("e"))
 {
 animation.Play("AirlockOpen", PlayMode.StopAll);
 }
 }
 }
 
 function OnGUI()
 {
 if (inside == true)
 {//Rect = posx,posy,width,height
 GUI.Box(Rect(0,0,64,64), image);
 }
 }

2: I don't think there is a way to reverse animations in Unity. But if you find something I would love to know! You can either: a) Use an external modelling / animating product to reverse the animation b) Code the door rotating c) Make another animation

I hope this helps! P.s. sorry the script is not formatted properly I can't make it work haha

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 AW0610AUT · Dec 15, 2013 at 03:08 PM 0
Share

Ok thanks i got that to work. But i still have 2 problems. 1.) The image doesn't disappear after i left the trigger.

  1. Solved can i display the image without the Grey box? Solved

  2. I used GUI.Lable ins$$anonymous$$d of Gui.Box

This is the code. I tried resetting the inside var if i am not inside the trigger but it doesn't work. I guess this function is only called when i am inside. Any idea how to make the image disappear when i left the trigger?

 var anim : String;
 var AnimationSource = gameObject;
 var inside : boolean = false;
 var image : Texture2D;
 
 function OnTriggerStay (col : Collider) //Using col and collider you can make it work ONLY for the door and not for any other triggers. Col will store the object hit.
 {
 if(col.gameObject.name == "First Person Controller")
     {
     inside = true;
     
  
         if (Input.Get$$anonymous$$eyDown("e"))
             {
             animation.Play("AirlockOpen", Play$$anonymous$$ode.StopAll);
             }
     }
     else { inside = false; }
 }
  
 function OnGUI()
 {
 if (inside == true)
 {//Rect = posx,posy,width,height
 GUI.Box(Rect(Screen.width/2-128,Screen.height-Screen.height/3,256,64), image);
 }
 }

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

17 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

Related Questions

Multiple Cars not working 1 Answer

How To Make GUI Buttons Load/Quit 1 Answer

animated sword + script 1 Answer

Two Unity GUI questions 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 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