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.
Thanks in advance!