Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

How to make a drawer in unity


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Drawer : MonoBehaviour
{
    private bool boxOpened;
    private bool coroutineAllowed;
    private Vector3 initialPosition;

    // Start is called before the first frame update
    void Start()
    {
        boxOpened = false;
        coroutineAllowed = true;
        initialPosition = transform.position;
    }
    void FixedUpdate()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }
    public void OnMouseDown()
    {
        Invoke("RunCoroutine", 0f);
    }

    private void RunCoroutine()
    {
        StartCoroutine("OpenThatDoor");
    }

    private IEnumerator OpenThatDoor()
    {
        coroutineAllowed = false;
        if (!boxOpened)
        {
            for (float i = 0f; i <= 1f; i += 0.4f)
            {
                transform.localPosition = new Vector3(transform.localPosition.x + 0.1f,
                    transform.localPosition.y,
                    transform.localPosition.z);
                yield return new WaitForSeconds(0f);
            }
            boxOpened = true;
        }
        else
        {
            for (float i = 1f; i >= 0f; i -= 0.4f)
            {
                transform.localPosition = new Vector3(transform.localPosition.x - 0.1f,
                    transform.localPosition.y,
                    transform.localPosition.z);
                yield return new WaitForSeconds(0f);
            }
            transform.position = initialPosition;
            boxOpened = false;
        }
        coroutineAllowed = true;
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: function on program stops unity 
Csharp :: ik not working unity 
Csharp :: IExtensionApplication autocad 
Csharp :: rb.addforce 3d c# 
Csharp :: how disable the back off a panel C# 
Csharp :: use different database with entitymanagerfactory 
Csharp :: razor: show editable list 
Csharp :: c# pass mouse events to parent 
Csharp :: git set origin url 
Html :: html meta redirect 
Html :: disable spell check html 
Html :: how to open link in a new tab 
Html :: bootstrap select box arrow not visible 
Html :: how to stop download option in video tag of HTML 
Html :: how to embed my website into Discord 
Html :: Uncaught ReferenceError: jQuery is not defined 
Html :: lorum picsum 
Html :: input type file csv only 
Html :: js import web3 cdn 
Html :: bootstrap center button horizontally 
Html :: stop video javascript 
Html :: back button ionic 
Html :: html option disabled 
Html :: font awesome 5 pro 
Html :: how to open in new page link html 
Html :: youtube iframe autoplay not working 
Html :: nbsp in html 
Html :: link email 
Html :: text color html 
Html :: datalayer push 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =