Why is OnUpdate being called twice here?

jfcarter

New member
Below is my custom script for hearing, I listen to an event that sets a variable. Its a pretty simple script. The issue is when I call the event the OnUpdate that plays next is played twice. So the debugs I see after the event is called are

1. Debug.Log("Hello1");
1. Debug.Log("Hello2");
1. Debug.Log("Hello3");
1. Debug.Log("Hello4 success");
1. Debug.Log("Hello5 failure");

This happens in the same tick (I tested by setting the behavior manager tick time to 5 so I can see that it is called twice). Below is a picture of my subTree, it is never playing the seek task because it immediately turns back to failure after a success. I have the sequence conditional set to Lower Priority not Self so I dont understand why its playing twice
 

Attachments

  • Orions_Reach - AI - Windows, Mac, Linux - Unity 2022.3.22f1 _DX11_ 11_18_2024 8_22_08 PM.png
    Orions_Reach - AI - Windows, Mac, Linux - Unity 2022.3.22f1 _DX11_ 11_18_2024 8_22_08 PM.png
    27.8 KB · Views: 2
public override void OnAwake()
{
// Subscribe to the audio played event from HearableAudioSource
HearableAudioSource.OnAudioSourcePlayed -= OnAudioSourcePlayed;
HearableAudioSource.OnAudioSourcePlayed += OnAudioSourcePlayed;
}

private void OnAudioSourcePlayed(Transform audioSourceTransform)
{
Debug.Log("Hello1");
//Debug.Log("Hearable audio detected");

// If the AI already heard the sound source, don't process it again
if (lastHeardSoundSource == audioSourceTransform.gameObject)
return;

// Get the position of the sound source
Vector3 soundPosition = audioSourceTransform.position;
Vector3 directionToSound = soundPosition - transform.position;

// Check if the sound is within hearing range
float distanceToSound = directionToSound.magnitude;
Debug.Log("Hello2");

if (distanceToSound > m_HearingDistance.Value)
return;

//// Perform a raycast to check if the sound is obstructed by any wall or obstacle
//RaycastHit hit;
//if (Physics.Raycast(transform.position, directionToSound, out hit, distanceToSound, m_IgnoreLayerMask))
//{
// if (hit.collider != null)
// {
// // If an obstacle is detected, ignore the sound
// return;
// }
//}

// If no closest target then set else see which sound is closer and set that one
if (lastHeardSoundSource == null)
{
// If the sound is within range and not blocked, trigger the detection logic
lastHeardSoundSource = audioSourceTransform.gameObject;
}
else
{
// Calculate the distance to the current closest target
float currentClosestDistance = Vector3.Distance(transform.position, lastHeardSoundSource.transform.position);

// Calculate the distance to the new sound
float newSoundDistance = Vector3.Distance(transform.position, audioSourceTransform.position);

// Check if the new sound is closer
if (newSoundDistance < currentClosestDistance)
{
// Update the closest target if the new sound is closer
lastHeardSoundSource = audioSourceTransform.gameObject;
}
}

Debug.Log("Hello3");
}

public override TaskStatus OnUpdate()
{
if (lastHeardSoundSource != null)
{
closestTargetPosition.Value = lastHeardSoundSource.transform.position;
lastHeardSoundSource = null;

Debug.Log("Hello4 success");
return TaskStatus.Success;
}

Debug.Log("Hello5 failure");
// If we have a closest target (sound), return success
return TaskStatus.Failure;
}
 
Likely because of conditional aborts. Conditional aborts call OnUpdate when it is being reevaluated, and then OnUpdate is called again when the task switches over and starts.
 
Back
Top