1

I made a door opening script in unity That's the code of it:

void Update () {

    if (i < 5)
    {
        if (Input.GetMouseButtonDown(0))
        {
            transform.Rotate(Vector3.up * Time.deltaTime * 1080);
            i++;

        }
    }
    else if (i >= 5)
    {
        if (Input.GetMouseButtonDown(0))
        {
            transform.Rotate(Vector3.down * Time.deltaTime * 5400);
            i = 0;
        }
    }


}

The problem is that action take place when I click the mouse everywhere and everytime. I want to make it happen only when I click the door.

Zannikos Ch
  • 31
  • 1
  • 5

2 Answers2

0

You want to use OnMouseDown() that uses the GameObject's collider.

0

You can use raycasts to detect what object are you clicking, or you can use the Event OnMouseDown(), like:

void OnMouseDown() {
    transform.Rotate(Vector3.up * Time.deltaTime * 1080);
}

OnMouseDown will not work on mobile devices