OnTriggerStay 不是每帧都调用
OnTriggerStay not called every frame
我有 2 个相同的游戏对象(板条箱 - 预制件)和 2 个需要放置板条箱的位置。当玩家将第一个箱子推到 spot 1 时,附加到该箱子的脚本中的 OnTriggerStay
激活并一直执行该工作(在控制台中打印)。问题是,当第二个板条箱到达第二个位置时,它会工作 20-30 次(打印)然后停止,然后我需要触摸它(稍微移动一下)然后它再次工作 20-30 次并停止。
这是附在板条箱上的脚本:
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);
}
}
}
}
}
OnTriggerStay 用于检测一个游戏对象何时仍在与另一个游戏对象接触一次。你不应该用它来检测游戏对象是否接触每一帧,因为有时它只被调用几次。
最好的方法是使用带有布尔变量的 Update
、OnTriggerEnterXXX
和 OnTriggerExitXXX
函数。在调用 OnTriggerEnterXXX
时将其设置为 true
,然后在调用 OnTriggerExitXXX
时将其设置为 false
。然后,您在 Update
函数中每帧检查此布尔变量。
bool triggered = false;
private void Update()
{
if (triggered)
{
//Do something!
}
}
void OnTriggerEnter2D(Collider2D collision)
{
Debug.Log("Entered");
if (collision.gameObject.CompareTag("SomeObject"))
{
triggered = true;
}
}
void OnTriggerExit2D(Collider2D collision)
{
Debug.Log("Exited");
if (collision.gameObject.CompareTag("SomeObject"))
{
triggered = false;
}
}
我有 2 个相同的游戏对象(板条箱 - 预制件)和 2 个需要放置板条箱的位置。当玩家将第一个箱子推到 spot 1 时,附加到该箱子的脚本中的 OnTriggerStay
激活并一直执行该工作(在控制台中打印)。问题是,当第二个板条箱到达第二个位置时,它会工作 20-30 次(打印)然后停止,然后我需要触摸它(稍微移动一下)然后它再次工作 20-30 次并停止。
这是附在板条箱上的脚本:
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);
}
}
}
}
}
OnTriggerStay 用于检测一个游戏对象何时仍在与另一个游戏对象接触一次。你不应该用它来检测游戏对象是否接触每一帧,因为有时它只被调用几次。
最好的方法是使用带有布尔变量的 Update
、OnTriggerEnterXXX
和 OnTriggerExitXXX
函数。在调用 OnTriggerEnterXXX
时将其设置为 true
,然后在调用 OnTriggerExitXXX
时将其设置为 false
。然后,您在 Update
函数中每帧检查此布尔变量。
bool triggered = false;
private void Update()
{
if (triggered)
{
//Do something!
}
}
void OnTriggerEnter2D(Collider2D collision)
{
Debug.Log("Entered");
if (collision.gameObject.CompareTag("SomeObject"))
{
triggered = true;
}
}
void OnTriggerExit2D(Collider2D collision)
{
Debug.Log("Exited");
if (collision.gameObject.CompareTag("SomeObject"))
{
triggered = false;
}
}