Search Unity

Opening doors with mouse drag (Amnesia-style)

Discussion in 'Scripting' started by ande04b, Sep 26, 2015.

  1. ande04b

    ande04b

    Joined:
    Aug 29, 2012
    Posts:
    119
    Hi people. :)

    I've been trying to make a script that allows me to open doors when I click and drag on a door.
    Right now, I'm using a raycast, and then I simply rotate the door when I click and drag on it. For some reason, however, there are still several flaws in it.
    When I just click it, and do not drag the mouse, some times the door still rotates.
    When I click and drag to open a door, it sometimes open "the wrong way" around its y-axis.

    Here is a short clip of how it works now:


    And here is the script:
    http://pastebin.com/wHCT21hU

    Does anyone have a better way to implement such a system? Or maybe just some hints as to how to make it better?

    thanks in advance :)
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Instead of relying on the raycast to continue to touch the door, try this process:

    1. When the player presses the button, project a raycast and see if it hits a door.
    2. If it does, store a reference to that door until the player releases the button.
    3. As the player moves, if the door reference != null open or close the door.
    4. If the player releases the button at any point, set the door reference to null.

    That way, the player doesn't have to continually look at the door to open or close it.
     
    Daahrien and ande04b like this.