Play Animation on Trigger Collision C#

Hi
So I’m trying to make a code so that when my Player enters the Collider surrounding the Animated Object, the animation plays.
I made the animation in Blender (imported in through the fbx, divvied up the animation clips properly in Unity as one does), and I’ve tried using both the Animator and Animation components (not together of course), tried adding private bool parameters into the code, tried using Trigger and Integer parameters in the Animator (again, not together), and I can’t figure out what I’m doing wrong.

My mesh is a base level with a few armatures and smaller meshes attached to it (all done in blender; the file includes the place itself, the doors+their hinge armatures, and basic furniture that I don’t need to interact with)–one of which is the armature and mesh for a door. The armature in question (ArmatureBedroom) has one bone (BedroomDoorHinge), and a mesh (BedroomDoor). In Blender, the Mesh is parented to the Armature and weighted to the Bone. In the Hierarchy of Unity, the Bone is parented to the Armature and the Mesh is a separate entry. All of this is parented to the main level (secondFloor). Right now, my Sphere Collider (the Trigger), Animator/Animation (I’ve been switching back and forth in my efforts), and DoorTrigger script (below) are all components of the Armature.
I have three animations for the door: BedroomIdle, BedroomOpen, and BedroomClose. With the import, they’re automatically assigned to the main level (secondFloor).

The Player (MCMod, tagged as Player), has a Character Controller, Mesh Collider, and Rigidbody. He works perfectly, so I’m hoping I don’t have to change anything on his end too much.

I’ve been fussing about with the same code, changing things and trying to get them to work for about a week with tutorials and such, but I started combining things as of yesterday since nothing straightforward has worked so far–so if the code is a terrible amalgam of things, that’s why.
Right now, this is what I have:

using UnityEngine;
using System.Collections;

public class BedroomDoorTrigger : MonoBehaviour {

    Collider collider;
    private Animation anim;
    public GameObject secondFloor;
    private bool parameterO = false;
    private bool parameterC = false;



    // Use this for initialization
    void Start () {
        anim = secondFloor.GetComponent<Animation> ();
        anim.Play ("BedroomNorm");
        parameterO = false;
        parameterC = false;
    }

    void OnTriggerEnter (Collider other) {
        if (other.tag == "Player") {
            parameterO = true;
            parameterC = false;

        }
    }

    void OnTriggerExit (Collider other) {
        if (other.tag == "Player") {
            parameterO = false;
            parameterC = true;
        }
    }

    void OnParameter (){
        if (parameterO = true) {
            anim = secondFloor.GetComponent <Animation> ();
            anim.Play ("BedroomOpen");
        }
        if (parameterC = true) {
            anim = secondFloor.GetComponent <Animation> ();
            anim.Play ("BedroomClose");
        }
    }
}

I’ve been trying to figure out how to make this work for about a week now, and it’s still not functioning properly. The animation refuses to play when my Player mesh enters the Trigger (Sphere Collider). It doesn’t interact with it at all. The Player can just walk right through the Trigger with nothing happening. I need to make it play the BedroomOpen animation when the character enters the Trigger, and play the BedroomClose animation when the character exits it. I don’t mind rewriting. I just need it to work. :confused:

Thanks in advance!

Looking at your code. Do you call OnParameter anywhere? It feels like your goal is after you do the OnTriggerEnter/OnTriggerExit, which sets your variables, you want to then call OnParameter to determine what should happen?

I had the

if (parameterO = true){
anim.Play ("BedroomOpen")
}
if (parameterC = true){
anim.Play ("BedroomClose")
}

not too long ago, but since it didn’t work, I deleted it. Ended up replacing it with what you see at the bottom (in the void OnParameter bit) later when I noticed the animations were linked to the secondFloor.

Sorry, I don’t understand. The first problem, that I’m seeing, is that you have an object entering a trigger and doing nothing other than setting variables. Assuming you have everything set up right for the trigger to happen, it never knows that it’s suppose to play an animation because OnParameter is not called.

All you’re doing, based on your code right now, is changing the bool variables.

Okay. That was actually helpful to understand what’s going on here right now.
I just tried cleaning up a bit and doing a better void OnParameter, but it’s still not working. I think I’m going to start the process over. I mean, it’s been a week. I’ll see where a clean slate gets me with this and post an update once I’ve got something cleaner.

Well, it shouldn’t be a problem to adjust your existing code.

After the if statement in OnTriggerEnter, if you add OnParameter(); does the animation not play?

Nope, not yet.
Here’s where I am.

using UnityEngine;
using System.Collections;

public class BedroomDoorTrigger2 : MonoBehaviour {

    Collider collider;
    private Animator anim;
    public GameObject gameObject;
    public GameObject resultObject;
    private bool parameterO = false;
    private bool parameterC = false;

    // Use this for initialization
    void Start () {
        parameterO = false;
        parameterC = false;
    }

    void OnTriggerEnter(Collider gameObject){
        if (gameObject.tag == "Player") {
            parameterO = true;
            parameterC = false;
        }
    }

    void OnParameter(){
        if (parameterO = true) {
            anim = resultObject.GetComponent (typeof(Animator)) ("BedroomOpen");
        }
        if (parameterC = true) {
            anim = resultObject.GetComponent (typeof(Animator)) ("BedroomClose");
        }
    }

    void OnTriggerExit (Collider other) {
        if (gameObject.tag == "Player") {
            parameterO = false;
            parameterC = true;
        }
    }
}

It’s giving me “error CS0119: Expression denotes a value', where a method group’ was expected”
I know it’s got to be talking about the (“BedroomOpen”)(“BedroomClose”) bit, but I’m unsure of how to fix that. I need to specify which animator clip to use, don’t I?

I’m not sure what you are trying to do in your getcomponent(typeof(animator)) calls. I’m pretty sure this is incorrect and probably the reason for your error.

You’re still not making a call to do anything though. When a “Player” enters the trigger collider you trigger OnTriggerEnter.
If this is setup correctly, you are setting parameterO to true and parameterC to false. And that’s it. Nothing else will happen at that point. You’ll need to add a call to your animation playing method (which in this case would be your OnParameter method) within that if statement that checks if gameObject.tag == “Player”.

Your onParameter method could than just use an if else statement that checks if parameterO is true or not. If true, you’ll want to play your animation, if false, you’ll want to play the other animation.

If you’re still stuck, I’ll try to help out tomorrow some, but this should get you headed in the right direction hopefully.