【发布时间】:2017-08-29 22:04:23
【问题描述】:
我有 2 个相同的游戏对象(板条箱 - 预制件)和 2 个需要放置板条箱的位置。当玩家将第一个板条箱推到点 1 时,附加到该板条箱的脚本中的 OnTriggerStay 会激活并一直执行该工作(在控制台中打印)。问题是,当第二个板条箱到达第二个位置时,它会执行 20-30 次(打印)然后停止,然后我需要触摸它(移动一点),然后它会再次执行 20-30 次并停止。
这是附加到 crates 的脚本:
public class Block : MonoBehaviour
{
public BlockType blockType;
public int blockId;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
private void OnTriggerStay2D(Collider2D col)
{
if (gameObject.GetComponent<Block>() != null)
{
if (col.GetComponent<Block>().blockType == BlockType.Crate && gameObject.GetComponent<Block>().blockType == BlockType.Point)
{
float distance = Vector3.Distance(this.transform.position, col.transform.position);
Debug.Log(col.GetComponent<Block>().blockId + ": " + distance); //for testing, distance is <= 0.05 and it doesn't display
if (distance <= 0.05)
{
PlayScene.CrateOnPoint(col.GetComponent<Block>().blockId);
Debug.Log("On place: " + col.GetComponent<Block>().blockId);
}
}
}
}
}
【问题讨论】: