【问题标题】:OnTriggerEnter2d unity not workingOnTriggerEnter2d 统一不起作用
【发布时间】:2016-04-03 08:05:42
【问题描述】:

我是使用 Unity 的新手,我正在尝试使用精灵作为碰撞触发器。 但我的 OnTriggerEnter2d 不会触发。 以下是信息:

主角信息

碰撞精灵用作触发信息

以及附在主角身上的 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# unity3d unity-components


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      您没有选中材质组件中的 isTrigger 复选框。

      【讨论】:

        猜你喜欢
        • 2014-12-14
        • 1970-01-01
        • 1970-01-01
        • 2018-10-13
        • 1970-01-01
        • 1970-01-01
        • 2021-11-17
        • 2015-05-12
        • 2014-05-27
        相关资源
        最近更新 更多