Unity OnTriggerEnter和OnTriggerExit延迟或无法在移动设备上运行

时间:2021-12-07 00:02:23

I am trying to determine when the player is grounded or not, and using the desktop player, the method described below works perfectly, but when compiled to Android behaves rather weirdly, I am not sure if it is just delayed or what, but the bool it is updating is rarely what it is supposed to be.

我试图确定播放器何时接地,并且使用桌面播放器,下面描述的方法工作得很好,但是当编译到Android时表现得相当奇怪,我不确定它是否只是延迟或什么,但是bool它正在更新很少是它应该是什么。

The code used to determine if the player is grounded:

用于确定玩家是否接地的代码:

void OnTriggerEnter2D(Collider2D other) {
    if (other.tag == "NormalGround") {
        player.Grounded = true;
    }
}

void OnTriggerExit2D(Collider2D other){
    if (other.tag == "NormalGround") {
        player.Grounded = false;
    }
}

On desktop this works perfectly, as demo'd here by writing the grounded var to a text box on Update():

在桌面上,这可以很好地工作,这里通过将接地变量写入Update()上的文本框来演示:

https://www.youtube.com/watch?v=CV0h18dz1cg

But on Android, it is almost as if the colliders are lagging:

但在Android上,几乎就像是对手一样滞后:

https://www.youtube.com/watch?v=c66uw2_NhrQ

You can see at the end of the video that the var only changes after I have made the character jump... (the true false text is updated every frame so it's not updated on Jump()

您可以在视频的末尾看到var仅在我跳过字符后才发生变化...(真正的假文本每帧都会更新,所以它不会在Jump()上更新

This is how the colliders are set up:

这就是碰撞器的设置方式:

The hierarchy:

Unity OnTriggerEnter和OnTriggerExit延迟或无法在移动设备上运行

The GO I am using for groundcheck:

我用于地面检查的GO:

Unity OnTriggerEnter和OnTriggerExit延迟或无法在移动设备上运行

How the platforms/ ground is set up:Unity OnTriggerEnter和OnTriggerExit延迟或无法在移动设备上运行

如何设置平台/地面:

1: The trigger used to detect when the player is grounded (visible in inspector)

1:用于检测播放器何时接地的触发器(在检查员中可见)

2: The colliders used to stop the player falling through the sprites (not in inspector)

2:用于阻止玩家通过精灵坠落的撞击器(不在检查员中)

The Jump() code:

Jump()代码:

    public void Jump ()
{
    if (!(PlayerDoubleJumped) && (Grounded) || ((!(Grounded) && (!PlayerJumped)))) {
        rb.velocity = new Vector2 (rb.velocity.x, JumpForce);
        animator.Play ("JumpCycle");
        PlayerJumped = true;
    } else if (!(Grounded) && (!(PlayerDoubleJumped)) && (PlayerJumped)) {
        rb.velocity = new Vector2 (rb.velocity.x, DoubleJumpForce);
        animator.Play ("JumpCycle");
        PlayerDoubleJumped = true;
    }
}

This is how I am moving the player:

这就是我移动玩家的方式:

this.transform.rotation = Quaternion.Euler (new Vector3 (0, 0, 0)); rb.velocity = new Vector2 (RunningSpeed, rb.velocity.y);

this.transform.rotation = Quaternion.Euler(new Vector3(0,0,0)); rb.velocity = new Vector2(RunningSpeed,rb.velocity.y);

Physics2D settings (default):

Physics2D设置(默认):

Unity OnTriggerEnter和OnTriggerExit延迟或无法在移动设备上运行

I am using Unity 5.5.0

我使用的是Unity 5.5.0

Does anyone have any idea what the problem is here?

有谁知道这里的问题是什么?

1 个解决方案

#1


0  

Possible fixes:

  • If you're using a mesh collider, switch to using a box collider. This works from time to time when mesh colliders are acting sketchy.

    如果您正在使用网格对撞机,请切换到使用盒式对撞机。当网格对撞机表现粗略时,这种情况会不时发挥作用。

  • Use OnTriggerStay instead of OnTriggerEnter.

    使用OnTriggerStay而不是OnTriggerEnter。

#1


0  

Possible fixes:

  • If you're using a mesh collider, switch to using a box collider. This works from time to time when mesh colliders are acting sketchy.

    如果您正在使用网格对撞机,请切换到使用盒式对撞机。当网格对撞机表现粗略时,这种情况会不时发挥作用。

  • Use OnTriggerStay instead of OnTriggerEnter.

    使用OnTriggerStay而不是OnTriggerEnter。