#Consistant-ish npc movement

1 messages · Page 1 of 1 (latest)

wise sand
gaunt walrus
#

!code

quartz iceBOT
gaunt walrus
#

Which one of those is "the function"?

wise sand
#

had a debug line before for time measuring

gaunt walrus
#

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;
    }
}
wise sand
#

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

gaunt walrus
#

Looks like it's being called every frame instead of every 2 seconds

wise sand
gaunt walrus
#

Yes but you said the function is called every 2 seconds

wise sand
#

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

gaunt walrus
#

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?

wise sand
gaunt walrus
#

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

wise sand
#

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

gaunt walrus
#

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;
}
wise sand
#

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

gaunt walrus
#

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)

wise sand
#

since you said at least for testing

gaunt walrus
#

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

wise sand
#

yea that also fixed it