Search Unity

Open kitchen cupboard on keypressed

Discussion in 'Scripting' started by Nagamiki, Jun 5, 2019.

  1. Nagamiki

    Nagamiki

    Joined:
    Jun 3, 2019
    Posts:
    2
    Hello!
    I have recently downloaded a cabinet kitchen → https://www.cgtrader.com/3d-models/interior/kitchen/ca-5cd9388f-0be1-4cea-ab44-bc1b493f590f
    So everything basically wors fine, however, I've added a player controller and I want for each cupboard, that the player press E to open it. I'm having some serious difficulties with that though since every cupboard opens instead of only one and I don't know how to focus on only one at the time, maybe adding a box collider for when the player is close to a cupboard?
    Plus, I've found two different types of keypressing :
    → Input.GetKeyDown(KeyCode.E) = which slightly opens every cupboard but not entirely
    → Input.GetKey("e") = which fully opens every cupboard but you have to keep pressing E and this is not what I want.

    I'm really new so here is what I've tried so far :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Door : MonoBehaviour {
    6.  
    7.     public bool isopen;
    8.     public float speed;
    9.  
    10.     [SerializeField]
    11.     float Open,Close;
    12.  
    13.  
    14.     private void Awake()
    15.     {
    16.        
    17.     }
    18.     void Start () {
    19.      
    20.     }
    21.    
    22.     // Update is called once per frame
    23.     void Update () {
    24.  
    25.         if (Input.GetKey("e"))
    26.         {
    27.             isopen = true;
    28.             Quaternion open = Quaternion.Euler(Open, 90, 90);
    29.             transform.localRotation = Quaternion.Slerp(transform.localRotation, open, Time.deltaTime * speed);
    30.         }
    31.      
    32.         else
    33.         {
    34.             isopen = false;
    35.             Quaternion close = Quaternion.Euler(Close, 90, 90);
    36.             transform.localRotation = Quaternion.Slerp(transform.localRotation, close, Time.deltaTime * speed);
    37.          
    38.         }
    39.     }
    40.  
    41.  
    42.     void PlayAnim()
    43.     {
    44.         print("hit");
    45.  
    46.         if (isopen)
    47.         {
    48.             isopen = false;
    49.         }
    50.  
    51.         else
    52.         {
    53.             isopen = true;
    54.         }
    55.  
    56.     }
    57. }
    58.  
    So, I would like to be able to open a single cupboard when needed, and press E again to close it.
    How am I supposed to do? Any directions?
    Thank you very much guys.
     
  2. Nagamiki

    Nagamiki

    Joined:
    Jun 3, 2019
    Posts:
    2
    I figured it out by myself, how do I delete this thread?
     
  3. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    789
    For anyone in the future who finds the thread, it is best to share your knowledge of how you solved it as well as not deleting the thread.

    Care to share?