- Home /
Question by
Han_Miyuu · Oct 30, 2021 at 05:48 AM ·
updateloopif-statementslogic
How to call unity event once every time my object reach destination in Update()?
bool isUnityEvent;
if (!isUnityEvent)
{
if (transform.position == TargetLocation)
{
onReachTarget.AddListener(Test);
onReachTarget.Invoke();
isUnityEvent = true;
}
if (transform.position != TargetLocation)
{
isUnityEvent = false;
}
}
Here i already make the unity event trigger once in update, but when i move the TargetLocation and my object reach that destination the unity event doesn't trigger.
Comment
Answer by Nikeneo · Nov 01, 2021 at 02:06 PM
Your transform must have the correct position in your code, it would be better to regulate this via a distance
float dist = Vector3.Distance(transform.position, TargetLocation);
if (dist < .5f)
{
onReachTarget.AddListener(Test);
onReachTarget.Invoke();
isUnityEvent = true;
}
Your answer
Follow this Question
Related Questions
Why is OnGUI not working? 1 Answer
Array Element in Foreach loop 1 Answer
Update Function Won't Do If Statements 1 Answer
While loop not executing fully 1 Answer