Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix enemies sometimes hearing thru walls #2681

Merged
merged 4 commits into from
Oct 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions Assets/Scripts/Game/EnemySenses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -923,24 +923,41 @@ bool CanSeeTarget(DaggerfallEntityBehaviour target)
return seen;
}

private static int defaultLayerOnlyMask = 0;
private static RaycastHit[] hitsBuffer = new RaycastHit[4];

bool CanHearTarget()
{
float hearingScale = 1f;

// If something is between enemy and target then return false (was reduce hearingScale by half), to minimize
// enemies walking against walls.
// Hearing is not impeded by doors or other non-static objects
RaycastHit hit;
Ray ray = new Ray(transform.position, directionToTarget);
if (Physics.Raycast(ray, out hit))
if (defaultLayerOnlyMask == 0)
defaultLayerOnlyMask = 1 << LayerMask.NameToLayer("Default");

// TODO: Modify this by how much noise the target is making
if (distanceToTarget < (HearingRadius * hearingScale) + mobile.Enemy.HearingModifier)
{
//DaggerfallEntityBehaviour entity = hit.transform.gameObject.GetComponent<DaggerfallEntityBehaviour>();
if (GameObjectHelper.IsStaticGeometry(hit.transform.gameObject))
return false;
// If something is between enemy and target then return false (was reduce hearingScale by half), to minimize
// enemies walking against walls.
// Hearing is not impeded by doors or other non-static objects
Ray ray = new Ray(transform.position, directionToTarget);
int nhits;
while (true) {
nhits = Physics.RaycastNonAlloc(ray, hitsBuffer, distanceToTarget, defaultLayerOnlyMask);
petchema marked this conversation as resolved.
Show resolved Hide resolved
if (nhits < hitsBuffer.Length)
break;
// hitsBuffer may have overflowed, retry with a larger buffer
hitsBuffer = new RaycastHit[hitsBuffer.Length * 2];
};
for (int i = 0; i < nhits; i++)
{
//DaggerfallEntityBehaviour entity = hit.transform.gameObject.GetComponent<DaggerfallEntityBehaviour>();
if (GameObjectHelper.IsStaticGeometry(hitsBuffer[i].transform.gameObject))
return false;
}
return true;
}

// TODO: Modify this by how much noise the target is making
return distanceToTarget < (HearingRadius * hearingScale) + mobile.Enemy.HearingModifier;
return false;
}

#endregion
Expand Down