Hello. I implemented my own code to open and close objects that are animatable. But a problem arose when it wouldnt work. The logic is as follows:
GameObject:Locker has the script, and the locker model has the animations. A box collider is attached to GameObject:Locker. When the player enters OnColliderEnter() is called and if the player presses “E” the locker should open. Only it doesnot, nor does it close. I think the problem is OnColliderEnter() method.
public class OpenClose : MonoBehaviour
{
Animation lockerBehavior;
public AudioClip open;
public AudioClip close;
bool isOpen;
bool isClosed = true;
void Start()
{
lockerBehavior = GetComponentInChildren<Animation>();
}
void Update ()
{
}
void OnColliderEnter(Collider enter)
{
if(enter.gameObject.tag == "Player")
{
if(isClosed == true)
{
if(Input.GetKey(KeyCode.E))
{
isOpen = true;
audio.PlayOneShot(open);
lockerBehavior["Open"].wrapMode = WrapMode.Once;
lockerBehavior.Play("Open");
if(isOpen == true)
{
if(Input.GetKey(KeyCode.E))
{
isClosed = true;
audio.PlayOneShot(open);
lockerBehavior["Close"].wrapMode = WrapMode.Once;
lockerBehavior.Play("Close");
}
}
}
}
}
}
}