Unity对撞机有时会经历其他对撞机吗? [英] Unity collider going through other colliders at times?

查看:103
本文介绍了Unity对撞机有时会经历其他对撞机吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个Pong克隆进行练习。我以2D模式设置了项目。

I'm trying to create a Pong clone for practice. I setup my project in 2D.

桨具有以下组件:


  • -Rigidbody2D(设置为 kinematic

  • -BoxCollider2D(是触发器

  • -Rigidbody2D (set to kinematic)
  • -BoxCollider2D (is a trigger)

球具有以下成分:


  • -Rigidbody2D(设置为 kinematic

  • -CircleCollider2D(是 trigger

  • -Rigidbody2D (set to kinematic)
  • -CircleCollider2D (is a trigger)

通过拖动(用户在屏幕上左右滑动手指)来控制操纵杆。我为此使用了 EasyTouch 插件。

The paddle is controlled via dragging (user dragging finger on screen left/right). I used the EasyTouch plugin for this.

然后,我使用以下脚本移动球:

Then I move the ball with this script:

void Update () {
        transform.position += new Vector3(xSpeed * Time.deltaTime, ySpeed * Time.deltaTime);
    }

这是我检测碰撞并在球碰到东西后重定向球的方式(水平对象是顶部/底部/桨,垂直对象是屏幕的左/右边框):

This is how I detect collisions and redirect the ball once it hits something (Horizontal objects are the top/bottom/paddle while Vertical objects are the left/right screen border):

void OnTriggerEnter2D(Collider2D c)
    {
        if(c.gameObject.tag.Equals("Horizontal"))
        {
            ySpeed *= -1;
        }
        else if(c.gameObject.tag.Equals("Vertical"))
        {
            xSpeed *= -1;
        }

    }

问题有时是球通过对最终用户来说可能会出现故障的拨片。我已经在网上进行了搜索,并尝试将刚体的碰撞检测属性设置为连续而不是离散。但是球仍然在某些时间穿过球拍。

The problem is sometimes the ball goes through the paddle which can look glitchy to the end-user. I've searched about this online and I've tried to set the rigidbody's Collision Detection property to Continuous instead of Discrete. But the ball still goes through the paddle at certain times.

有人知道如何解决吗?

Anyone know how to solve this? What am I doing wrong with how I setup/coded my game?

谢谢

推荐答案

您有一个非常简单的概念错误。
OnTriggerEnter2D(Collider2D)用于获取对撞机是否已进入其他对撞机。换句话说,您可以遍历对象。

You have a very simple concept error. OnTriggerEnter2D(Collider2D) is to get if the collider has entered other collider. In other words you can go through the object.

您需要使用以下功能:

OnCollisionEnter2D(Collision2D coll)

我建议您观看此Unity教程,因为它说明了所有这一切都很好: https://unity3d.com/learn/tutorials/ modules / beginner / physics / colliders

I suggest you to watch this Unity tutorial because it explains all this really good: https://unity3d.com/learn/tutorials/modules/beginner/physics/colliders

其他信息: http://docs.unity3d.com/ScriptReference/Collider2D.html

谢谢。

这篇关于Unity对撞机有时会经历其他对撞机吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆