I’m trying to trigger animations with the function OnTriggerEnter. I want the player to hit the button and trigger the door animation. I have included a little diagram of what I am wanting to do.
I’m using unity 5.2.
I’ve already tried the code below. I put this code on the door parent, with the animation applied to the door parent. But when the player hits the button it doesn’t trigger the animation.
Code:
using UnityEngine;
using System.Collections;
public class Door1 : MonoBehaviour {
Animator animator;
bool doorOpen;
void Start()
{
doorOpen = false;
animator = GetComponent<Animator>();
}
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "Player")
{
doorOpen = true;
DoorControl ("Open");
}
}
void DoorControl(string direction)
{
animator.SetTrigger (direction);
}
}
I’ve also tried other code with it attached to the button and the collider set to “is trigger”, but that didn’t work either.
Try putting a debug.log into the trigger part & in your doorcontrol so you can determine that it is being triggered & is calling the door control function. If they are then the issue is likely in your animation bit
Put a break point on your if statement in the ontriggerenter. When it triggers hover the mouse over tag & see what it is showing. Also pause it when the player is over the button & check that they are actually colliding.