Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
879 views
in Technique[技术] by (71.8m points)

unity3d - Collision callback function not called

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 Configurations The Spears Configurations This 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 working Thanks for all the answers (BTW sorry for my bad english I am austrian) enter image description here

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

enter image description here

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:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...