OnTriggerStay不会在每一帧都调用 [英] OnTriggerStay not called every frame
问题描述
我有2个相同的GameObjects(弹坑-预制件)和2个需要包装箱的位置.当玩家将第一个板条箱推到位置1时,该板条箱所附脚本中的OnTriggerStay
会激活并一直执行该工作(在控制台中打印).问题是,当第二个板条箱到达第二个位置时,它会工作20-30次(打印)然后停止,那么我只需触摸它(稍微移动一下),然后它又会工作20-30次并停止./p>
以下是附在包装箱上的脚本:
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用于检测GameObject何时仍一次与另一个GameObject接触.您不应将其用作检测GameObjects是否触摸每一帧的方法,因为有时它只会被调用几次.
执行此操作的最佳方法是使用带有布尔变量的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;
}
}
I have 2 identical GameObjects (crates - prefabs) and 2 spots where that crates need to get. When player push first crate to spot 1, OnTriggerStay
in script attached to that crate activate and do the job all time (print in console). Problem is that when second crate get to second spot, it do job 20-30 times (print) and then stop, then I need to just touch it (move a little) and again it does job 20-30 times and stop.
Here is script attached to 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);
}
}
}
}
}
OnTriggerStay is used to detect when a GameObject is still touching with another GameObject once. You should not use it as a way to detect if GameObjects are touching every frame because sometimes, it is only called few times.
The best way to do this is with the Update
, OnTriggerEnterXXX
and the OnTriggerExitXXX
functions with a boolean variable. You set it to true
when in the OnTriggerEnterXXX
is called then to false
when OnTriggerExitXXX
is called. You then check this boolean variable every frame in the Update
function.
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;
}
}
这篇关于OnTriggerStay不会在每一帧都调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!