Search Unity

Door Opening when clicked...

Discussion in 'Scripting' started by xdacholl, Apr 5, 2010.

  1. xdacholl

    xdacholl

    Joined:
    Mar 30, 2010
    Posts:
    7
    Hi,
    I have been trying to make the door model that I built and animated in Maya to open when I clicked on to it..

    I posted a question a few days ago, and got one kindly reply..(Thank you!) so .. I tried to write a java script based on the tip the person gave to me..

    I wrote..

    function Update() {
    }

    function OnMouseDown() {

    animation.Play ("opening door");

    }


    that is pretty much it.. it didnt work.. then my head went blank again. I have to say I dont have any programming experience in my life..
    Someone help me ?

    :( :( :(
     
  2. Ramen Sama

    Ramen Sama

    Joined:
    Mar 28, 2009
    Posts:
    561
    well.. do you have an animation called "opening door" setup? Also you need a collision object setup to detect the raycast called by the mouse down event. you'd need something like a box collider.
     
  3. SarperS

    SarperS

    Joined:
    Mar 10, 2009
    Posts:
    824
    I just answered a question like this today, you can find the same code below, hope this helps.

    Please search the forum before posting in the future.

    EDIT: It has to be placed in your Update() function, and Door1 in the code should be named the same with your door game object, and also the door has to have a collider of any sorts.

    Code (csharp):
    1. var hit:RaycastHit;
    2. var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    3.    
    4.    if(Input.GetMouseButtonDown(0))
    5.    {
    6.       if(Physics.Raycast( ray, hit, 50 ))
    7.       {
    8.          switch(hit.transform.name)
    9.          {
    10.             case "Door1":
    11.             Door1.animation.Play ("opening door");
    12.             break;
    13.            
    14.             case "Door2":
    15.             Door2.animation.Play ("opening door");
    16.             break;
    17.          }
    18.       }
    19.    }
     
  4. xdacholl

    xdacholl

    Joined:
    Mar 30, 2010
    Posts:
    7
    Yes! I had the animation set up "opening door" and had a collider as well. Looking at thet code in the reply... I would never be able to write something like that on my own.. (well at least for now.. ) but thank you very much and I'll go try it immediately..



    oh.. and it's pretty hard to find a post with the answers I wanted... maybe i'm not using write key words for researching.. ;(

    anyway, thank you..

    Another question.. what exactly is

    function Update() {} ??
    Ive read the java script page on unity web page.. it doesnt really make a big sense to me..
     
  5. Ramen Sama

    Ramen Sama

    Joined:
    Mar 28, 2009
    Posts:
    561
  6. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    This is my door script. It's a little more involved. First of all, the door will glow when the mouse is over it (while you are close to it). Also, there are separate animations depending on if the door swings left or right, and if the door is opening or closing.

    Also, it works on regular doors and bathroom stall doors, which for some reason I wanted to swing differently. After you attach the script do the door, just change the public variables doorType and doorHand in the inspector window to the right type.

    Public var Shopper is really the Player. Drag and drop your player onto the variable Shopper in the inspector window.

    One cool thing is that there is no Update() function, so I think it's efficient as far as performance goes.

    I put this script on the door itself along with a mesh collider:

    Code (csharp):
    1.  
    2. public var isOpen : boolean = false;
    3. public var doorType : String = "door";
    4. public var doorHand : String = "rh";
    5. private var originalColor;
    6. var highlightMultiply = 1.50;
    7. var shopper;
    8.  
    9. function Start() {
    10.       originalColor = renderer.material.color;  
    11.       shopper = GameObject.FindWithTag("Player");
    12. }
    13.  
    14. function OpenDoor(){
    15.     if(!isOpen){
    16.         if(doorType == "door"){
    17.             if(doorHand == "rh"){
    18.                 animation["RHDoorOpen"].wrapMode = WrapMode.Once;
    19.                 animation.CrossFade("RHDoorOpen", 0.3);
    20.             } else if(doorHand == "lh") {
    21.                 animation["LHDoorOpen"].wrapMode = WrapMode.Once;
    22.                 animation.CrossFade("LHDoorOpen", 0.3);
    23.             }
    24.         } else if(doorType == "stall"){
    25.             if(doorHand == "rh"){
    26.                 animation["RHStallOpen"].wrapMode = WrapMode.Once;
    27.                 animation.CrossFade("RHStallOpen", 0.3);
    28.             } else if(doorHand == "lh") {
    29.                 animation["LHStallOpen"].wrapMode = WrapMode.Once;
    30.                 animation.CrossFade("LHStallOpen", 0.3);
    31.             }
    32.         }
    33.     }
    34.     isOpen = true;
    35. }
    36.  
    37. function CloseDoor(){
    38.     if(isOpen){
    39.         if(doorType == "door"){
    40.             if(doorHand == "rh"){
    41.                 animation["RHDoorClose"].wrapMode = WrapMode.Once;
    42.                 animation.CrossFade("RHDoorClose", 0.3);
    43.             } else if(doorHand == "lh") {
    44.                 animation["LHDoorClose"].wrapMode = WrapMode.Once;
    45.                 animation.CrossFade("LHDoorClose", 0.3);
    46.             }
    47.         } else if(doorType == "stall"){
    48.             if(doorHand == "rh"){
    49.                 animation["RHStallClose"].wrapMode = WrapMode.Once;
    50.                 animation.CrossFade("RHStallClose", 0.3);
    51.             } else if(doorHand == "lh") {
    52.                 animation["LHStallClose"].wrapMode = WrapMode.Once;
    53.                 animation.CrossFade("LHStallClose", 0.3);
    54.             }
    55.         }
    56.         isOpen = false;
    57.     }
    58. }
    59.  
    60. function OnMouseOver() {
    61.     var dist = Vector3.Distance(shopper.transform.position, transform.position);
    62.     if(dist < 3.2){
    63.         renderer.material.color.g = originalColor.g*highlightMultiply;
    64.         renderer.material.color.b = originalColor.b*highlightMultiply;
    65.         renderer.material.color.r = originalColor.r*highlightMultiply;
    66.     }
    67. }
    68.  
    69. function OnMouseExit() {
    70.     renderer.material.color = originalColor;
    71. }
    72.  
    73. function OnMouseDown() {
    74. var dist = Vector3.Distance(shopper.transform.position, transform.position);
    75.     if(dist < 3.2){
    76.         if(isOpen){
    77.             CloseDoor();
    78.         } else {
    79.             OpenDoor();
    80.         }
    81.     }
    82. }
     
  7. xdacholl

    xdacholl

    Joined:
    Mar 30, 2010
    Posts:
    7

    Hi Ive tried the code. I am just wondering if you could explain your code a bit more? Like what it does ... and so on? With the code the door opens.. but it keep returns error message "unknown identifier: "Door1". and same for Door2. I named my door game object as Door1.. but why does it keep returning the same error?? and what is the Door2 for??
     
  8. xdacholl

    xdacholl

    Joined:
    Mar 30, 2010
    Posts:
    7


    It is certainly a wonderful feeling that I feel less uncomfortable with looking at long script like this!!! hehe I've been watching and following tutorials (The FTS shooting tutorial in Unity page?) and I can understand it a bit more... Certainly I will try this as well.. Thank you so much. Ill might return with hips of other questions... but thank you!