Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Open door on click

Discussion in 'Animation' started by unity_BF40D87EF19971307D44, Oct 17, 2021.

  1. unity_BF40D87EF19971307D44

    unity_BF40D87EF19971307D44

    Joined:
    Aug 23, 2021
    Posts:
    3
    Hello, I have a door in my scene that I would like to open on click. The door itself (Café1Porte) has an empty GameObject (Café1Porte_Pivot) attached as a pivot point with an attached animator/controller to create the rotating motion for the door (so that the door looks like it's opening on a hinge rather than rotating around around the center). I have a simple script using void OnMouseDown to play the animation clip of the door opening, but the issue is that the door only opens properly when I click on Café1Porte_Pivot, that is to say the specific part of the door where I have attached the pivot point. I have tried putting the same script on Café1Porte to see if it would play the animation, but when I click on the door the object rotates around the center rather than around the pivot point. I am wondering whether there is a way to play the door opening animation (door rotating around the pivot point) while clicking on the door itself rather than the pivot point? I'm using editor version 2019.4.29f1. Any help would be greatly appreciated!
     
  2. razzraziel

    razzraziel

    Joined:
    Sep 13, 2018
    Posts:
    395
    put script on pivot, define a public collider on it. assign door's collider to that. check if your mouse click hits door's collider.
     
  3. unity_BF40D87EF19971307D44

    unity_BF40D87EF19971307D44

    Joined:
    Aug 23, 2021
    Posts:
    3
    Thanks for the reply! Since I'm a novice, would you mind explaining more in detail how to do all of that? I have added the door box collider to the hinge, but how do I check if my mouse hit clicks the door's collider? Appreciate your help!
     
  4. razzraziel

    razzraziel

    Joined:
    Sep 13, 2018
    Posts:
    395
    Code (CSharp):
    1. public class StartOnClick : MonoBehaviour
    2.     {
    3.         public Collider clickObject;
    4.         public Animation anim;
    5.  
    6.         private Ray _ray;
    7.         private RaycastHit _hit;
    8.         private bool _opened;
    9.  
    10.         private void Update()
    11.         {
    12.             if (Input.GetKeyDown(KeyCode.Mouse0))
    13.             {
    14.                 Click();
    15.             }
    16.         }
    17.  
    18.         private void Click()
    19.         {
    20.             if (!clickObject) return;
    21.             if (!anim) return;
    22.  
    23.             _ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    24.  
    25.             if (Physics.Raycast(_ray, out _hit))
    26.             {
    27.                 if (_hit.collider == clickObject)
    28.                 {
    29.                     if (_opened)
    30.                     {
    31.                         anim.Play("doorClose");
    32.                         _opened = false;
    33.                     }
    34.                     else
    35.                     {
    36.                         anim.Play("doorOpen");
    37.                         _opened = true;
    38.                     }
    39.                  
    40.                 }
    41.             }
    42.         }
    43.     }
     
    Last edited: Oct 18, 2021
  5. unity_BF40D87EF19971307D44

    unity_BF40D87EF19971307D44

    Joined:
    Aug 23, 2021
    Posts:
    3
    It works! Thank you so much for your help!!