my npc always moves first after 11 calls of the function, it takes 22 seconds each call being every 2 seconds, i cant figure out why
https://paste.mod.gg/ihiurcznpako/0
A tool for sharing your source code with the world!
1 messages · Page 1 of 1 (latest)
my npc always moves first after 11 calls of the function, it takes 22 seconds each call being every 2 seconds, i cant figure out why
https://paste.mod.gg/ihiurcznpako/0
A tool for sharing your source code with the world!
!code
Use links to services like:
https://paste.mod.gg/, https://hastebin.skyra.pw/, https://paste.ofcode.org/, https://paste.myst.rs/
Surround code with three backquotes. Not quotation marks.
To format as C#, add cs to the first line:
```cs
// Your code here
```
Add a comment with a line number if there is an error message.
Which one of those is "the function"?
oh forgot to mention, mb but the HandleRoaming() function handles idle movement when the enemy is not chasing or attacking the player
had a debug line before for time measuring
Put in this debug and see what it prints:
private void HandleRoaming() {
Debug.Log($"Time.time: {Time.time}, lastWanderTime: {lastWanderTime}, wanderTime: {wanderTime}");
if (Time.time - lastWanderTime >= wanderTime) {
agent.destination = getRandomPoint(transform.position, range);
lastWanderTime = Time.time;
}
}
this is the latest, and it moved at the top one in the picture
and when i ran it again it only moved around 16s
Looks like it's being called every frame instead of every 2 seconds
it is outside the check, i put it where you put it in the code block you sent
Yes but you said the function is called every 2 seconds
i did say that.. i need to learn to be more specific
ok let me rephrase so its more specific and helpful to you so you can help me, i want it to wander every X seconds, in this case for debugging its set to 2 seconds originally its 5 seconds, but what im guessing happens when it reaches the wander time is it picks the same location because even if i put the debug message inside the if statement, still prints but doesnt move the enemy
Next log how far away the random point is from the agent's current position
and are you sure that just setting the agent destination is enough to make it move?
the range is 10 but i should prob set a minimum distance
Also in the getRandomPoint function if the random point isn't on a navmesh then it just returns the agent's current position. So it's expected that most of the time it won't move anywhere
depending how much of the area the navmesh covers
this is where its currently placed
anyways let me add the debug rq since im on pc now
huh, appears to be returning center
i think 10 is too big
You could try this to make sure the random position always finds a spot (at least for testing):
private Vector3 getRandomPoint(Vector3 center, float range) {
for(int i = 0; i < 100; ++i)
{
Vector3 randomPoint = center + Random.insideUnitSphere * range;
NavMeshHit hit;
if (NavMesh.SamplePosition(randomPoint, out hit, 1f, NavMesh.AllAreas)) {
return hit.position;
}
}
return center;
}
always on start it seems to find a point that is not the center
but the next few are just center
re-ran it to check consistancy
mvoed 4 times this time
I don't know how SamplePosition does it but since you're getting a point inside a sphere the point can be above or below the navmesh. It should be Random.insideUnitCircle instead (with y converted to z)
while this fixed it, im assuming its not the best practice for released games?
since you said at least for testing
No, it's just to confirm what the problem is
Do it with insideUnitCircle instead
Vector2 randomDistance = Random.insideUnitCircle * range;
Vector3 randomPoint = new Vector3(center.x + randomDistance.x, center.y, center.z + randomDistance.y);
You can still keep the for loop because in the actual game the navmesh likely won't cover everything
yea that also fixed it