Search Unity

Opening door through key script help required

Discussion in 'Scripting' started by LionWare, Jan 25, 2015.

  1. LionWare

    LionWare

    Joined:
    Jan 7, 2015
    Posts:
    9
    We are making a horror game. Still haven't named it yet but there seems to be a place where we are stuck. We need to open a door with a key. We created the scripts and they were working just fine but the problem is that we need to make a separate script for all the animations and name them according to the script's variables.and that too isn't working all the time. It is giving the error "The animation state could not be played because it counld not be found" So we would really appreciate your help in fixing this bug. Any Help in this regard would be appreciated. Thank You in Advance! The Script is given below
    Code (JavaScript):
    1. #pragma strict
    2. var doorClip : AnimationClip;
    3. var Key : GameObject;
    4. private var Door = false;
    5.  
    6. function Start ()
    7. {
    8.  
    9. }
    10.  
    11. function Update ()
    12. {
    13.     if (Input.GetKeyDown(KeyCode.E) && Door == true && Key.active == false)
    14.     {
    15.     GameObject.Find("Door").animation.Play("Cupboardopen");
    16.     }
    17. }
    18.  
    19. function OnTriggerEnter (theCollider : Collider)
    20. {
    21.     if (theCollider.tag == "Player")
    22.     {
    23.         Door = true;
    24.     }
    25. }
    26.  
    27. function OnTriggerExit (theCollider : Collider)
    28. {
    29.     if (theCollider.tag == "Player")
    30.     {
    31.         Door = false;
    32.     }
    33. }
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    I'm not sure if this is it, but remember update is being called over and over, so you want to only open the door once:
    Code (csharp):
    1.  
    2. [LIST=1]
    3. [*]    if (Input.GetKeyDown(KeyCode.E) && Door == true && Key.active == false)
    4. [*]    {
    5. [*]    Door = false;
    6.  
    7. [*]    GameObject.Find("Door").animation.Play("Cupboardopen");
    8. [*]    }
    9. [/LIST]
    10.  
     
  3. Stiffx

    Stiffx

    Joined:
    Oct 27, 2014
    Posts:
    455
    I would also create a script to hold key identifier. For example
    script with a Array of keys and their ID.
    every door that needs to be open with a key holds a script that asks if you have that key ID in your inventory.

    so script Keys (array item: Key name (string), Key ID(int), (hasTheKey true/false))
    when you trigger the door and press the key, foreach your array and see if the door keyId is the same as the array item, then check if this item hasTheKey is true.

    :)
     
  4. LionWare

    LionWare

    Joined:
    Jan 7, 2015
    Posts:
    9
    We already have made a script for the key The script destroys the key when we press "e" in the trigger zone it destroys the key and the script on the door checks whether the key is destroyed or not. The only problem is arising from the animation but thanks for giving your view. It is well appreciated.
     
  5. LionWare

    LionWare

    Joined:
    Jan 7, 2015
    Posts:
    9
    The update doesn't seem to be the error, we tried to do it your way but still the error of animation stays the same
     
  6. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Imo you should use an origin point and rotate it around that, no animation needed that way.
     
  7. LionWare

    LionWare

    Joined:
    Jan 7, 2015
    Posts:
    9
    Do you have any script to do that because we haven't learnt it yet? By the way thanks!
     
  8. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Basic example, infinitely rotates but you get the idea if you try it :)

    Code (CSharp):
    1. public GameObject door;
    2. public Transform doorPivot;
    3.  
    4.  
    5. void Update(){
    6. Door.transform.RotateAround(doorPivot,Vector3.Up,20 * Time.deltaTime);
    7. }
     
  9. LionWare

    LionWare

    Joined:
    Jan 7, 2015
    Posts:
    9
    We seem to have got it. We tweaked our previous code with the pivot thing and it's working S thank you all.. Here is our new code
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var door: Transform;
    4. var angleOpen: int;
    5. var angleClose: int;
    6. var speedOpen: int = 1000;
    7. var Key : GameObject;
    8. private var Door = false;
    9.  
    10. function Start ()
    11. {
    12.  
    13. }
    14.  
    15. function Update ()
    16. {
    17.     if (Input.GetKey(KeyCode.E) && Door == true && Key.active == false)
    18.     {     if (door.transform.localEulerAngles.y < angleOpen){
    19.          door.transform.Rotate(Vector3.up*Time.deltaTime*speedOpen);
    20.          }
    21.     }
    22. }
    23.  
    24. function OnTriggerEnter (theCollider : Collider)
    25. {
    26.     if (theCollider.tag == "Player")
    27.     {
    28.         Door = true;
    29.     }
    30. }
    31.  
    32. function OnTriggerExit (theCollider : Collider)
    33. {
    34.     if (theCollider.tag == "Player")
    35.     {
    36.         Door = false;
    37.     }
    38. }