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

Interactive Doors and Objects.

Discussion in 'Scripting' started by squeakywafflez, Jun 9, 2014.

  1. squeakywafflez

    squeakywafflez

    Joined:
    Jun 9, 2014
    Posts:
    4
    Okay, so the whole "Press E to open door" Thing isn't really sitting with me right now...
    And I know that I can get a pretty simple "drag to open door with mouse" script going with some rigidbodies on a hinge or on a box or whatever, But there's an issue that I don't even know how to approach.
    Sounds
    When I open the door, it wouldn't be right if it played just one sound because the speed of the door depends on how fast the player wll drag it.
    And what about when a player drops the box on the ground, and it makes a noise? Or when it slides on the ground, it'll make a different noise.

    Help?
     
  2. vegenarie

    vegenarie

    Joined:
    Jan 5, 2011
    Posts:
    287
    I would try with different sound intensities for the door depending on how strong or fast they moved it.
    For an object dropped on the ground i would check the rigidbody velocity magnitude to change the sound intensity and a short noise that can be repeated without noticing any difference, in the same way you use a rain clip that repeats over and over.
     
  3. squeakywafflez

    squeakywafflez

    Joined:
    Jun 9, 2014
    Posts:
    4
    Well. Yeah.
    But how the hell would I get it all to work? I'm no programmer...
     
  4. vegenarie

    vegenarie

    Joined:
    Jan 5, 2011
    Posts:
    287
    You need to know how to program if plan to make a game with Unity3D, what i told you is very easy to do if know a bit about programming. For instance, to change audio intensity you use in C#: audio.volume=1f; for maximum volume, you can change it the value from 0 to 1
    If want to know the velocity magnitude of a rigidbody : rigidbody.velocity.magnitude
     
  5. expressionpixel

    expressionpixel

    Joined:
    Oct 1, 2012
    Posts:
    20
    The design pattern you are looking for is the term "context game programming". The idea would be you enter a trigger volume near an object and the gui pops up with "Press E to xxxx". So all these interactive objects would inherit an Interface and define all the same functions but each object will do different things and play different sounds but it will keep the same execution.

    Code (CSharp):
    1. public interface IContext
    2. {
    3.   void ShowPopUp();
    4.   void OnPressE();
    5. }
    6.  
    7. examples:
    8.  
    9. class Door : Monobehaviour, IContext
    10. ...
    11. class Lightswitch : Monobehaviour, IContext
    12. ...
    13.  
    14.  
    In your player class you have to make a reference variable to IContext m_context and assign it when you enter this trigger, you should also make a new tag called "Context" and see if the trigger you entered is of Context tag and assign it to m_context by getting the IContext component, then in your player update code check if you press E and if m_context isn't null and call OnPressE(). So if you are near a Door it will execute all the code from class Door or if it is near a LightSwitch, and so forth.
    Edit: Specifically the term is contextual button.
     
  6. squeakywafflez

    squeakywafflez

    Joined:
    Jun 9, 2014
    Posts:
    4
    But I said I don't want to press E to open it .w.
     
  7. squeakywafflez

    squeakywafflez

    Joined:
    Jun 9, 2014
    Posts:
    4
    I'll see what I can do with this on my own.