Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Door open scrip

Discussion in 'Scripting' started by ddulshan, Aug 9, 2014.

  1. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Hey guys.

    I wonna make a script for door opening. When the player gets about 1M near to the door a message display(a GUI message) "Press E to open door" or something. Please help. I'm no expert in scripting.
     
  2. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    I don't think you need to be an expert to write this script, and you certainly don't need to be one to at least try ;)

    You probably want to star with using a Collider (set as a trigger) with a script attached to it to that:
    1. Checks when the player enters or exits it, and keeps track of that state.
    2. Shows the "Press E to Open" message (using OnGUI), based on the state from above.
    3. Opens your door when the correct key is pressed and the player is in the correct state.

    If this seems too complex, perhaps following a few of the tutorials in the Learn section might give you more insight into how to approach things in Unity, and help you help yourself. Less time waiting for help = more time making games!
     
  3. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    @NomadKing : That happens whn only player touches the door right?
     
  4. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    You'd probably want to do that check in the OnTriggerEnter and OnTriggerExit calls, by checking the Tag of what is entering, and only doing stuff if it's the 'Player'.

    EDIT: If you're meaning triggering the collider would only happen when touching the door, what I mean is that you have a second GameObject that you make into a Trigger and place it around the door to whatever size you want to be able to interact with it from. The door itself would still have its own Collider, but just for physics interaction.
     
  5. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    @NomadKing : Ok, can u write a example script? Pls.
     
  6. herman111

    herman111

    Joined:
    May 11, 2014
    Posts:
    119
  7. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    @herman111 : My connection is not good, so would you mind writing down a simple script.
     
  8. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    It's such a simple script, that writing an example would be writing the entire script for you, and there's a reason I chose to not do that in the first reply.

    The scripting forum is for helping people who are stuck with a scripting problem, not writing other peoples projects for them. You have to be at least willing to try stuff.
     
  9. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Ok. Thanks. Any final advice?
     
  10. herman111

    herman111

    Joined:
    May 11, 2014
    Posts:
    119
    try this


    to make text appear...make a emptyGameObject and add a box collider , check trigger.
    when player enters box,OnTriggerStay...the text is enabled

    when player exits box,OnTriggerExit...the text is disabled
     

    Attached Files:

  11. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Thanks man. Realy.
     
  12. herman111

    herman111

    Joined:
    May 11, 2014
    Posts:
    119
    no problem