本文介绍了碰撞回调函数未调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Unity的血腥初学者,目前正在研究2D Brawler.该运动效果很好,但是我的对撞机没有执行应做的事情...我想检测两个GameObjects是否撞撞(Spear和Player2),以及撞撞的Player2s healthPoints是否应通过Spears AttackDamage降低.

GameObjects的名称也是它们的标签. Spears Prefab具有以下配置:SpriteRendered(材料Sprites-Default),BoxCollider2D(材料无物理材料2D,IsTrigger(未激活),UsedByEffector(也未激活),Rigidbody2D(运动,无材料,模拟(已激活),KinematicContacts(已激活) ),其余为标准配置))

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpearCtr : MonoBehaviour {

    public Vector2 speed;
    public float delay;
    Rigidbody2D rb;

    void Start ()
    {
        rb = GetComponent<Rigidbody2D>();
        rb.velocity = speed;
        Destroy(gameObject, delay);
    }
    void Update ()
    {
        rb.velocity = speed;
    }
}

播放器配置矛的配置这是我之前尝试过的代码

OnCollision2D(Collision2D target);
{
    if (target.gameObject.tag == "Spear")
    {
        hp = -1;
        if (hp <= 0)
        {
            alive = false;
        }
    }
}

我希望有人能告诉我如何使它工作感谢所有的答案(顺便说一句我英语不好我是奥地利人)在此处输入图片描述

在此处输入图片描述

解决方案

OnCollisionEnter()无效的原因:

科里森:

1 . Rigidbody Rigidbody2D 未附加.

如果至少是两个游戏对象中的一个,如果它是3D游戏对象,则必须附加Rigidbody.如果它是2D GameObject/2D对撞机,则应该附加Rigidbody2D.

2 .拼写错误

您拼写错误.它的拼写也区分大小写.

正确的拼写:

对于3D MeshRenderer/Collider :

OnCollisionEnter

OnCollisionStay

OnCollisionExit

对于2D SpriteRenderer/Collider2D :

OnCollisionEnter2D

OnCollisionStay2D

OnCollisionExit2D

3 .对撞机已选中IsTrigger. 取消选中,以便调用OnCollisionXXX函数.

4 .该脚本未附加到任何碰撞游戏对象.将脚本附加到GameObject.

5 .您为回调函数提供了错误的参数.

对于3D MeshRenderer/Collider :

参数是Collision而不是Collider.

是:

void OnCollisionEnter(Collision collision) {}

不是

void OnCollisionEnter(Collider collision) {}

对于2D SpriteRenderer/Collider2D :

6.碰撞的两个刚体都启用了isKinematic.在这种情况下,将不会调用回调函数.

这是完整的Collison表:

i am bloody beginner with Unity and i am currently working on a 2D Brawler. The movement works perfectly but my colliders don't do what they should... I want to detect if two GameObjects Collide (Spear and Player2) and if the collide Player2s healthPoints should decrease by Spears AttackDamage.

The names of the GameObjects are also their tags. The Spears Prefab has following configuration: SpriteRendered(Material Sprites-Default), BoxCollider2D(Material None Physics Material 2D, IsTrigger(not activated), UsedByEffector(also not activated) Rigidbody2D(Kinematic, None Material, Simulated(Activated), KinematicContacts(activated), Standard configs for the rest))

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpearCtr : MonoBehaviour {

    public Vector2 speed;
    public float delay;
    Rigidbody2D rb;

    void Start ()
    {
        rb = GetComponent<Rigidbody2D>();
        rb.velocity = speed;
        Destroy(gameObject, delay);
    }
    void Update ()
    {
        rb.velocity = speed;
    }
}

The Players ConfigurationsThe Spears ConfigurationsThis was the code i have tried before

OnCollision2D(Collision2D target);
{
    if (target.gameObject.tag == "Spear")
    {
        hp = -1;
        if (hp <= 0)
        {
            alive = false;
        }
    }
}

I hope someone can tell me how to get this workingThanks for all the answers(BTW sorry for my bad english I am austrian)enter image description here

enter image description here

解决方案

Reasons why OnCollisionEnter() does not work:

Collison:

1.Rigidbody or Rigidbody2D is not attached.

At-least, one of the two GameObjects must have Rigidbody attached to it if it is a 3D GameObject. Rigidbody2D should be attached if it is a 2D GameObject/2D Collider.

2.Incorrect Spelling

You failed to spell it right. Its spelling is also case sensitive.

The correct Spellings:

For 3D MeshRenderer/Collider:

OnCollisionEnter

OnCollisionStay

OnCollisionExit

For 2D SpriteRenderer/Collider2D:

OnCollisionEnter2D

OnCollisionStay2D

OnCollisionExit2D

3.Collider has IsTrigger checked. Uncheck this for the OnCollisionXXX functions to be called.

4.The script is not attached to any of the Colliding GameObjects. Attach the script to the GameObject.

5.You provided the wrong parameter to the callback functions.

For 3D MeshRenderer/Collider:

The parameter is Collision not Collider.

It is:

void OnCollisionEnter(Collision collision) {}

not

void OnCollisionEnter(Collider collision) {}

For 2D SpriteRenderer/Collider2D:

6.Both Rigidbody that collides has a isKinematic enabled. The callback function will not be called in this case.

This is the complete collison table:

这篇关于碰撞回调函数未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 23:24