[SOLVED] Need OnTriggerEnter help

Hi guys,

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.

I would truly appreciate any help.

Thanks in advance.

Does at least the player or button have a rigid body & do you have the tag on the player (I often forget to set that)?

Yes i have all of those.

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

I used your advice but it didn’t show up in the console when the player entered the trigger.
this is my 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");
            Debug.Log (123);
        }
    }

    void DoorControl(string direction)
    {
        animator.SetTrigger (direction);
        Debug.Log (321);
    }
}

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.

I did that but still nothing showed up.

And this is the image when i paused the game but it didn’t show anything.

Can you put in the inspector shot of the player as well please?

Here.

What happens if you take the rigid body off the door trigger? It looks like it should be working though

Nope still doesn’t trigger nor show in the console :frowning:

I fixed the problem by upgrading to unity 5.2.2 but then it cause this problem:

When the player hits the trigger it says this in the console. Parameter type ‘Open’ does not match. UnityEngine.Animator:SetTrigger(String).

I fixed that problem by changing the parameter on my animation from an int. to a trigger.