Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Problem animating Prefab Doors using SetBool

Discussion in 'Animation' started by KevalrOxy, Aug 29, 2015.

  1. KevalrOxy

    KevalrOxy

    Joined:
    Aug 15, 2015
    Posts:
    21
    FIXED...

    I have lots of prefab doors in my scene. The prefab has a box collider component set as a Trigger. It also has a script attached to the prefab to handle behaviour etc. My FPS character also has a box collider attached to it. When my FPS box collider enters ‘OnTriggerEnter’ a door collider, the script attached to the door detects the FPS collider and it triggers an open animation (plays an animation using an ‘animator.SetBool(“OpenTrigger”, true);’. This all works great... except...

    My problem is this. Any of the prefab triggers that detect the FPS trigger only ever activates one of the doors, and always the same one. I can walk into any trigger on any door and the trigger will work, but only on ‘the’ one door will animate. How do I get it to open the door that I’m actually standing in front of and not another prefab? The prefab has a GameObject parent and the door is a child.Here’s my code…

    I've tried so many things. Any help is greatly appreciated...

    public class DoorController : MonoBehaviour {

    public Animator animator;
    public bool DoorHandleDetected = false;
    public bool DoorJammed;
    public bool DoorLocked;

    void Start () {
    animator = GameObject.FindObjectOfType<Animator>();​
    }​

    void OnTriggerEnter(Collider collider) {
    if (collider.gameObject.name == "PlayerArm") {
    DoorHandleDetected = true;
    IsItJammed();​
    }​
    }​

    void OnTriggerExit(Collider collider) {
    DoorHandleDetected = false;
    }​

    void IsItJammed() {
    if (DoorJammed == true) {
    print ("This door is jammed");
    return;
    }​

    print ("This door can be opened");
    animator.SetBool("OpenTrigger",true); // "OpenTrigger" is the name of the trigger in the animator
    }​
    }
     
    Last edited: Aug 29, 2015
  2. KevalrOxy

    KevalrOxy

    Joined:
    Aug 15, 2015
    Posts:
    21
    BUMP..

    Upon further investigation, when the game is running and I use the scene window to manually set the OpenTrigger in the Animator, on each prefab, they all work fine. So the problem must be on a script level in its abiltiy to know what prefab i'm communicating with. I have almost lost all of my hair and my eyes look like pits in my face... please help :)
     
  3. KevalrOxy

    KevalrOxy

    Joined:
    Aug 15, 2015
    Posts:
    21
    FIXED ... ahhhhh

    It's ok, I fixed it. Jeez, i'm such a numpty...

    It's a componet of the object...

    Such a relief... doors work great now :)