[英]OnTriggerEnter2d unity not working
我是使用Unity的新手,並且正在嘗試使用Sprite作為碰撞觸發器。 但是我的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 ;_;");
從來沒有
我真的不知道我在做什么錯。 謝謝
看來您錯誤拼寫了void OnTriggerEnter2d(Collider2D other)
。 它實際上應該是OnTriggerEnter2D
。 請注意,此處為大寫D。 像OnMouseDown()
這樣的觸發器和對撞機的所有名稱都以相同的方式工作-它們必須具有完全相同的名稱,區分大小寫。
您沒有在材料組件中選中isTrigger復選框。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.