简体   繁体   English

每次我使用 OnCollisionEnter2D 它都不起作用

[英]Every time I use OnCollisionEnter2D it does not work

I want to make that the ball every time it hits the paddles increase its velocity but it does not works我想让球每次碰到桨时都会增加它的速度,但它不起作用

ball.cs球.cs


using UnityEngine;

public class Ball : MonoBehaviour
{
    [SerializeField] private float velocityMultiplier = 1.1f;

    private void OnCollisioEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Paddle"))
        {
            ballRb.velocity *= velocityMultiplier;
        }
    }
}

Here is the Paddle A inspector这是 Paddle A 检查员

PaddleA桨A

and Paddle B is the same和桨B是一样的

Your colldier seems to be marked as Kinematic .您的 colldier 似乎被标记为Kinematic It does not respond to colliders, and can only be moved with Scripts.它不响应对撞机,只能使用脚本移动。

From docs:来自文档:

Kinematic Rigidbody Collider运动学刚体对撞机

This is a GameObject with a Collider and a kinematic Rigidbody attached (ie, the IsKinematic property of the Rigidbody is enabled).这是一个附有碰撞器和运动刚体的游戏对象(即,刚体的 IsKinematic 属性已启用)。 You can move a kinematic rigidbody object from a script by modifying its Transform Component but it will not respond to collisions and forces like a non-kinematic rigidbody.您可以通过修改其变换组件从脚本中移动运动刚体对象,但它不会像非运动刚体那样响应碰撞和力。 Kinematic rigidbodies should be used for colliders that can be moved or disabled/enabled occasionally but that should otherwise behave like static colliders.运动学刚体应该用于偶尔可以移动或禁用/启用的碰撞器,但在其他方面应该表现得像静态碰撞器。 An example of this is a sliding door that should normally act as an immovable physical obstacle but can be opened when necessary.这方面的一个例子是滑动门,它通常应该作为一个不可移动的物理障碍,但可以在必要时打开。 Unlike a static collider, a moving kinematic rigidbody will apply friction to other objects and will “wake up” other rigidbodies when they make contact.与静态对撞机不同,移动的运动刚体将对其他物体施加摩擦,并在它们接触时“唤醒”其他刚体。

Source: https://docs.unity3d.com/Manual/CollidersOverview.html来源: https ://docs.unity3d.com/Manual/CollidersOverview.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM