Open door on click

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!

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.

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!

public class StartOnClick : MonoBehaviour
    {
        public Collider clickObject;
        public Animation anim;

        private Ray _ray;
        private RaycastHit _hit;
        private bool _opened;

        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.Mouse0))
            {
                Click();
            }
        }

        private void Click()
        {
            if (!clickObject) return;
            if (!anim) return;

            _ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(_ray, out _hit))
            {
                if (_hit.collider == clickObject)
                {
                    if (_opened)
                    {
                        anim.Play("doorClose");
                        _opened = false;
                    }
                    else
                    {
                        anim.Play("doorOpen");
                        _opened = true;
                    }
                  
                }
            }
        }
    }

BlindUnpleasantGadwall

1 Like

It works! Thank you so much for your help!!