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

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

问题描述

我正在尝试创建一个用于练习的 Pong 克隆.我在 2D 中设置我的项目.

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

桨具有以下组件:

  • -Rigidbody2D(设置为kinematic)
  • -BoxCollider2D(是一个触发器)

球有以下组成部分:

  • -Rigidbody2D(设置为kinematic)
  • -CircleCollider2D(是一个触发器)

桨通过拖动控制(用户在屏幕左/右拖动手指).为此,我使用了 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;
        }

    }

问题是有时球会穿过桨,这对最终用户来说可能会出现故障.我在网上搜索过这个,我尝试将刚体的 Collision Detection 属性设置为 Continuous 而不是 Discrete.但是球在某些时候仍然会穿过桨.

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天全站免登陆