我是使用Unity的新手,并且正在尝试使用Sprite作为碰撞触发器。
但是我的OnTriggerEnter2d不会触发。
这是信息:

主角信息
c# - OnTriggerEnter2d统一无法正常工作-LMLPHP

碰撞精灵用作触发信息
c# - OnTriggerEnter2d统一无法正常工作-LMLPHP

以及附加到主字符上的newPlatformRow脚本的代码

using UnityEngine;
using System.Collections;

public class NewPlatformRow : MonoBehaviour {

    private float leftPlatformX = -12f; //de unde incepe platforma din stanga
    private float rightPlatformX = 3.123f; // de unde incepe platforma din dreapta
    private float rowDistance = -5f; //distanta dintre randurile de platforme

    private float leftPlatformWidth; //acestea vor fi calculate random pt fiecare rand nou
    private float rightPlatformWidth;





    // Use this for initialization
    void Start ()
    {
        //Debug.Log("why wont you work ;_;");

    }

    // Update is called once per frame
    void Update () {
        //Debug.Log("why wont you work ;_;");
    }


    void OnTriggerEnter2d(Collider2D other)
    {

        Debug.Log("why wont you work ;_;");

        if (other.gameObject.CompareTag("newPlatformRow"))
        {

            Vector3 newLeftPlaformPosition;
            Vector3 newRightPlaformPosition;

            var leftPlatform = GameObject.Find("LeftWallPlatform");
            var rightPlatform = GameObject.Find("RightWallPlatform");

            newLeftPlaformPosition = new Vector3(leftPlatform.transform.position.x, leftPlatform.transform.position.y + rowDistance, leftPlatform.transform.position.z);
            newRightPlaformPosition = new Vector3(rightPlatform.transform.position.x, rightPlatform.transform.position.y + rowDistance, rightPlatform.transform.position.z);

            Transform leftPlatformTransform = leftPlatform.transform;
            Transform rightPlatformTransform = rightPlatform.transform;

            Transform newLeftPlatform = Instantiate(leftPlatformTransform, newLeftPlaformPosition, leftPlatformTransform.rotation) as Transform;
            Transform newRightPlatform = Instantiate(rightPlatformTransform, newLeftPlaformPosition, rightPlatformTransform.rotation) as Transform;

            newLeftPlatform.parent = leftPlatformTransform.parent;
            newRightPlatform.parent = rightPlatformTransform.parent;

        }
    }
}


并不是说:Debug.Log("why wont you work ;_;");它从未被调用

完整的场景信息:
c# - OnTriggerEnter2d统一无法正常工作-LMLPHP

我真的不知道我在做什么错。
谢谢

最佳答案

看来您拼错了void OnTriggerEnter2d(Collider2D other)
它实际上应该是OnTriggerEnter2D。请注意,此处为大写D。触发器和对撞机(如OnMouseDown())的所有名称都以相同的方式工作-它们必须具有完全相同的名称,区分大小写。

关于c# - OnTriggerEnter2d统一无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34511046/

10-12 00:31