C# 如何使用两个相同的长方体碰撞器打开相同的动画?

C# 如何使用两个相同的长方体碰撞器打开相同的动画?,c#,unity3d,C#,Unity3d,我在游戏中有一个关卡,当你进入一个盒子对撞机时,一个入口打开,当你离开它时,它关闭。当我进入框碰撞器1时,两个入口都需要打开,当我离开时,两个入口都需要关闭。当我进入第二个入口的框碰撞器2时,这也需要发生。我有一个用于box collider 1的脚本,并将其应用于box collider 2。它检查玩家是否在对撞机中。我有一个动画师bool,它直接从框碰撞器脚本中获取变量以检入范围。我用那玩意儿做动画。然而,那个动画师布尔森并不为《盒子碰撞2》工作。“长方体碰撞器”(box collider)

我在游戏中有一个关卡,当你进入一个盒子对撞机时,一个入口打开,当你离开它时,它关闭。当我进入框碰撞器1时,两个入口都需要打开,当我离开时,两个入口都需要关闭。当我进入第二个入口的框碰撞器2时,这也需要发生。我有一个用于box collider 1的脚本,并将其应用于box collider 2。它检查玩家是否在对撞机中。我有一个动画师bool,它直接从框碰撞器脚本中获取变量以检入范围。我用那玩意儿做动画。然而,那个动画师布尔森并不为《盒子碰撞2》工作。“长方体碰撞器”(box collider)的变量可以工作,但动画师布尔(animator bool)不能工作。有没有办法连接第二个,或者我需要为那个盒子碰撞器制作一个新的脚本

长方体碰撞器代码:

门户脚本代码:

一幅情景图:

首先,您不应该在每一帧都使用GetComponent。无论是喜欢动画,你都应该立即保存它。或者,您可以简单地将portalOrder设置为portalOrder类型,然后在通过Inspector引用时自动设置相应的引用

然后是,当前您只更新一个动画师。为了控制它们,你必须以某种方式连接它们

我会这样做

public class Portal : MonoBehaviour 
{
    private Animator anim;
    private bool inPortalRange;
    // Public read-only access
    public bool InPortalRange => inPortalRange;

    // Reference each other via the Inspector in both portals
    public Portal OtherPortal;

    // Give this directly the according type so you don't need GetComponent at all
    public PortalBorder portalBorder;

    // I would recommend to do things always as early as possible
    // Awake is executed before Start
    private void Awake()
    {
        anim = GetComponent<Animator>();
    }

    private void Update ()
    {
        OpenPortal();
        UpdateAnimation();
    }

    private void UpdateAnimation()
    {
        // Here now use the range of either this or the other portal
        anim.SetBool("inPortalRange", InPortalRange || OtherPortal.inPortalRange );
    }

    private void OpenPortal()
    {
        inPortalRange = portalBorder.inRange;
    }
}
现在,您的脚本必须像单击按钮一样,可以通过检查器或使用代码添加回调

public class Portal : MonoBehaviour 
{
    public Animator anim;
    private bool inPortalRange;
    // Public read-only access
    public bool InPortalRange => inPortalRange;

    // Reference each other via the Inspector in both portals
    public Portal OtherPortal;

    // Give this directly the according type so you don't need GetComponent at all
    public PortalBorder portalBorder;

    // I would recommend to do things always as early as possible
    // Awake is executed before Start
    private void Awake()
    {
        anim = GetComponent<Animator>();

        // Instead of checking a bool in Update simply
        // wait until the according events get invoked
        portalBorder.OnEnteredPortalRange.AddListener(EnablePortal);
        portalBorder.OnLeftPortalRange.AddListener(DisablePortal);
    }

    private void OnDestroy()
    {
        // always make sure to remove callbacks when not needed anymore
        // in roder to avoid NullReferenceExceptions
        portalBorder.OnEnteredPortalRange.RemoveListener(EnablePortal);
        portalBorder.OnLeftPortalRange.RemoveListener(DisablePortal);
    }

    public void EnablePortal()
    {
        anim.SetBool("inPortalRange", true);
        OtherPortal.anim.SetBool("inPortalRange", true);
    }

    public void DisablePortal()
    {
        anim.SetBool("inPortalRange", false);
        OtherPortal.anim.SetBool("inPortalRange", false);
    }
}

请不要添加与问题无关的标签。这个问题与C语言没有任何关系,所以不应该添加标记。
public class Portal : MonoBehaviour 
{
    private Animator anim;
    private bool inPortalRange;
    // Public read-only access
    public bool InPortalRange => inPortalRange;

    // Reference each other via the Inspector in both portals
    public Portal OtherPortal;

    // Give this directly the according type so you don't need GetComponent at all
    public PortalBorder portalBorder;

    // I would recommend to do things always as early as possible
    // Awake is executed before Start
    private void Awake()
    {
        anim = GetComponent<Animator>();
    }

    private void Update ()
    {
        OpenPortal();
        UpdateAnimation();
    }

    private void UpdateAnimation()
    {
        // Here now use the range of either this or the other portal
        anim.SetBool("inPortalRange", InPortalRange || OtherPortal.inPortalRange );
    }

    private void OpenPortal()
    {
        inPortalRange = portalBorder.inRange;
    }
}
public class PortalBorder : MonoBehaviour
{
    public UnityEvent OnEnteredPortalRange;
    public UnityEvent OnLeftPortalRange;

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
        {
            OnEnteredPortalRange.Invoke();
        }
    }

    private void OnTriggerExit2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
        {
            OnEnteredPortalRange.Invoke();
        }
    } 
}
public class Portal : MonoBehaviour 
{
    public Animator anim;
    private bool inPortalRange;
    // Public read-only access
    public bool InPortalRange => inPortalRange;

    // Reference each other via the Inspector in both portals
    public Portal OtherPortal;

    // Give this directly the according type so you don't need GetComponent at all
    public PortalBorder portalBorder;

    // I would recommend to do things always as early as possible
    // Awake is executed before Start
    private void Awake()
    {
        anim = GetComponent<Animator>();

        // Instead of checking a bool in Update simply
        // wait until the according events get invoked
        portalBorder.OnEnteredPortalRange.AddListener(EnablePortal);
        portalBorder.OnLeftPortalRange.AddListener(DisablePortal);
    }

    private void OnDestroy()
    {
        // always make sure to remove callbacks when not needed anymore
        // in roder to avoid NullReferenceExceptions
        portalBorder.OnEnteredPortalRange.RemoveListener(EnablePortal);
        portalBorder.OnLeftPortalRange.RemoveListener(DisablePortal);
    }

    public void EnablePortal()
    {
        anim.SetBool("inPortalRange", true);
        OtherPortal.anim.SetBool("inPortalRange", true);
    }

    public void DisablePortal()
    {
        anim.SetBool("inPortalRange", false);
        OtherPortal.anim.SetBool("inPortalRange", false);
    }
}