Unity可以在参考脚本中检查碰撞吗?

发布于 2025-02-06 07:57:47 字数 2000 浏览 1 评论 0原文

如您在图中所见,我们正在编码角色掉落时重新创建的逻辑。 当前,当您输入ISLEFT和ISRIGHT脚本时,位置将变为真,相反的一侧是错误的。 如果落在地板上的碰撞变为真实,我们将重生的角色真实。 管理此依据的脚本。

public class isFall : MonoBehaviour
{
    public GameObject hood;
    public bool isRight , isLeft;

    public IsRight Position_Right; 
    public IsLeft Position_Left;
    bool isDelay;
    public Health hp;

    private void Awake() {
        isRight = isLeft = false;
        isDelay = false;
    }

    private void Update() {
        if(isDelay){
            StartCoroutine(WaitForSpawn());
            isDelay = false;
        }

        if (Position_Right.isRight)
        {
            isRight = true;
            isLeft = false;
            Position_Left.isLeft = false;
        }

        if (Position_Left.isLeft)
        {
            isLeft = true;
            isRight = false;
            Position_Right.isRight = false;
        }
    }

    private void OnTriggerEnter2D(Collider2D collision) {
        if (collision.gameObject.tag == "Player"){
            isDelay = true;
            hp.TakeDamage(1);
        }
    }

    IEnumerator WaitForSpawn()
    {
        yield return new WaitForSeconds(2.0f);
        if (isLeft) hood.transform.position = Position_Left.transform.position;
        else hood.transform.position = Position_Right.transform.position; 
    }

}

// ------------------------------------------------------------------------- ----------------------------------

public class IsLeft : MonoBehaviour
{
    public bool isLeft = false;

    private void OnTriggerEnter2D(Collider2D collider) {
        if (collider.CompareTag("Player")){
            Debug.Log(" OnTriggerEnter2D = Enter");
            isLeft = true;
        }
    }
}

iSfall可以检测到Isleft和Isigrt ontriggerenter2d吗? 该脚本正常, 但是我认为,如果我用一个脚本引用它,最好以后进行管理,所以我想知道如何进行管理。

As you can see in the figure, we are coding the logic of re-creating when the character falls off.
Currently, when you enter the isLeft and isRight scripts, the position becomes true and the opposite side is false.
If the collisions that fall on the floor become true, we respawn the characters where they are true.
The script that manages this isFall.

public class isFall : MonoBehaviour
{
    public GameObject hood;
    public bool isRight , isLeft;

    public IsRight Position_Right; 
    public IsLeft Position_Left;
    bool isDelay;
    public Health hp;

    private void Awake() {
        isRight = isLeft = false;
        isDelay = false;
    }

    private void Update() {
        if(isDelay){
            StartCoroutine(WaitForSpawn());
            isDelay = false;
        }

        if (Position_Right.isRight)
        {
            isRight = true;
            isLeft = false;
            Position_Left.isLeft = false;
        }

        if (Position_Left.isLeft)
        {
            isLeft = true;
            isRight = false;
            Position_Right.isRight = false;
        }
    }

    private void OnTriggerEnter2D(Collider2D collision) {
        if (collision.gameObject.tag == "Player"){
            isDelay = true;
            hp.TakeDamage(1);
        }
    }

    IEnumerator WaitForSpawn()
    {
        yield return new WaitForSeconds(2.0f);
        if (isLeft) hood.transform.position = Position_Left.transform.position;
        else hood.transform.position = Position_Right.transform.position; 
    }

}

//---------------------------------------------------------------------------

public class IsLeft : MonoBehaviour
{
    public bool isLeft = false;

    private void OnTriggerEnter2D(Collider2D collider) {
        if (collider.CompareTag("Player")){
            Debug.Log(" OnTriggerEnter2D = Enter");
            isLeft = true;
        }
    }
}

Can isFall detect isLeft and isRight OnTriggerEnter2D?
The script works fine,
But I think it would be better to manage it later if I reference it with one script, so I want to know how.

reference image

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟柳画桥 2025-02-13 07:57:47

不,您不能单独使用isfall仅实现这一目标,因为 ontriggerenter2d 说:在另一个对象输入附加到此对象的触发器对撞机时发送的。,您必须在左或右对撞机上附加脚本以触发回调。

因此,如果您为角色创建脚本,它将能够检测所有山脉。

public class Character: MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D collider) {
        if (collider.CompareTag("Left"))
            isLeft = true;
        else if (collider.CompareTag("Right"))
            isLeft = false;
        else if (collider.CompareTag("Fall"))
            StartCoroutine(WaitForSpawn());
    }
}

顺便说一句,一个字段isleft就足够了,因为重生位置是左或右。

No you can't achieve this with isFall alone, because the documentation of OnTriggerEnter2D says: Sent when another object enters a trigger collider attached to this object. You must attach a script on the left or right collider to trigger the callback.

So... if you create a script for the character, it is able to detect all the colliders.

public class Character: MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D collider) {
        if (collider.CompareTag("Left"))
            isLeft = true;
        else if (collider.CompareTag("Right"))
            isLeft = false;
        else if (collider.CompareTag("Fall"))
            StartCoroutine(WaitForSpawn());
    }
}

BTW a single field isLeft is enough, because the respawn position is either left or right.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文