#Walk In Place Workaround

1 messages ยท Page 1 of 1 (latest)

lost blaze
#

By @chilly wind

Walk In Place Workaround: When your AI Boar just won't move and walk in place: I found a fix for that: You have to modify AIStateRoaming.cs and AIStateRoamingTemplate.cs as follows:

using UnityEngine;
using UnityEngine.Serialization;

namespace BLINK.RPGBuilder.AI
{
    [Serializable, CreateAssetMenu(fileName = "New AI Roaming State Template", menuName = "BLINK/AI/Templates/Roaming Template")]
    public class AIStateRoamingTemplate : AIStateIdleTemplate
    {
        public float MinDistanceToMove = 2f; //Snake

        public float PauseDuration = 60;
        public float RoamDistance = 15;
        public float RoamPointThreshold;
        public LayerMask roamGroundLayers;
        public float roamTargetCheckInterval = 0.25f;
        public bool roamAroundSpawner = true;
    }
}```

First add the new variable as per above to AIStateRoamingTemplate.
Next, update AIStateRoaming.cs as follows:
**SEE ATTACHED MSG/TEXT FILE AT BOTTOM OF THIS POST, SCRIPT WAS TOO BIG TO FIT IN 1 MESSAGE**

Here is a summary:
Updated logic in the final versions of both AIStateRoaming and AIStateRoamingTemplate scripts:

AIStateRoamingTemplate: A new MinDistanceToMove property was added to the template to set a minimum distance threshold for the AI entity to start moving. This property helps to prevent the AI entity from walking in place when the roaming target is too close.
```public float MinDistanceToMove = 1f;```

AIStateRoaming: In the Execute method, the AI entity checks if it is not paused, and the distance to the roam target is greater than both the RoamPointThreshold and MinDistanceToMove. If these conditions are met, the AI entity moves towards the roaming point. Otherwise, the AI entity resets its movement.
csharp
Copy code
```if (!IsRoamingPaused && distanceToTarget > roamingTemplate.RoamPointThreshold && distanceToTarget > roamingTemplate.MinDistanceToMove)
{
    ThisAIEntity.MoveAgent(RoamTargetPosition);
}
else
{
    ThisAIEntity.ResetMovement();
}```

After calling ThisAIEntity.ResetMovement(), the AI entity checks if its movement has stopped by examining the magnitude of its agent's velocity. If the movement hasn't stopped, the AI entity generates a new roaming point by calling the GetNewRoamingPoint() method, which makes the AI entity attempt to move to the new point.
```if (ThisAIEntity.EntityAgent.velocity.magnitude > 0.1f)
{
    GetNewRoamingPoint();
}```

The updated logic aims to prevent the AI entity from walking in place and ensures that the AI entity continues moving even if it initially fails to reset its movement. 
Updated logic in the final versions of both AIStateRoaming and AIStateRoamingTemplate scripts:

AIStateRoamingTemplate: A new MinDistanceToMove property was added to the template to set a minimum distance threshold for the AI entity to start moving. This property helps to prevent the AI entity from walking in place when the roaming target is too close.
```public float MinDistanceToMove = 1f;```

AIStateRoaming: In the Execute method, the AI entity checks if it is not paused, and the distance to the roam target is greater than both the RoamPointThreshold and MinDistanceToMove. If these conditions are met, the AI entity moves towards the roaming point. Otherwise, the AI entity resets its movement.
csharp
Copy code
```if (!IsRoamingPaused && distanceToTarget > roamingTemplate.RoamPointThreshold && distanceToTarget > roamingTemplate.MinDistanceToMove)
{
    ThisAIEntity.MoveAgent(RoamTargetPosition);
}
else
{
    ThisAIEntity.ResetMovement();
}```

After calling ThisAIEntity.ResetMovement(), the AI entity checks if its movement has stopped by examining the magnitude of its agent's velocity. If the movement hasn't stopped, the AI entity generates a new roaming point by calling the GetNewRoamingPoint() method, which makes the AI entity attempt to move to the new point.
```if (ThisAIEntity.EntityAgent.velocity.magnitude > 0.1f)
{
    GetNewRoamingPoint();
}```
#

The updated logic aims to prevent the AI entity from walking in place and ensures that the AI entity continues moving even if it initially fails to reset its movement.
NOTE: Not fully tested, but does seem to work for most cases so far.

#

Original location: #rpgb-coding message

last bit didnt fit haha, 5k chars with nitro and had 75 left at end ๐Ÿ˜‚
lemme know if i missed anything @chilly wind

chilly wind
#

So here is a newer version, that attempts to reset the AI as follows as a last resort. In this updated Execute method, the AI entity will be reset to its spawned position when it hasn't moved more than 3 meters in the last 10 seconds.```public override AIState Execute()
{
CombatEntity newTarget = ThisAIEntity.SearchTarget(roamingTemplate.viewDistance, roamingTemplate.viewAngle, roamingTemplate.AutoAggroDistance);
if (newTarget != null)
{
ThisAIEntity.ThisCombatEntity.SetTarget(newTarget);
return ThisAIEntity.GetChaseState();
}

if (ThisAIEntity.IsPlayerInteractionState()) return this;
HandleRoaming();

float distanceToTarget = Vector3.Distance(ThisAIEntity.transform.position, RoamTargetPosition);
if (!IsRoamingPaused && distanceToTarget > roamingTemplate.RoamPointThreshold && distanceToTarget > roamingTemplate.MinDistanceToMove)
{
    ThisAIEntity.MoveAgent(RoamTargetPosition);
}
else
{
    ThisAIEntity.ResetMovement();
}

timeSinceLastPositionCheck += Time.deltaTime;
if (timeSinceLastPositionCheck >= 10f)
{
    float distanceMoved = Vector3.Distance(previousPosition, ThisAIEntity.transform.position);
    if (distanceMoved < 3f)
    {
        Debug.Log("AI boar has moved less than 3 meters in the last 10 seconds. Resetting AI entity to its spawned position.");
        ThisAIEntity.transform.position = spawnedPosition;
        GetNewRoamingPoint();
    }
    else
    {
        previousPosition = ThisAIEntity.transform.position;
    }
    timeSinceLastPositionCheck = 0f;
}

return this;

}

#

Note: Update the Execute method to move the AI entity back to its spawned position instead of destroying it:

#

also: Update the Enter method to store the initial position:

#
{
    IsActive = true;
    InitMovement();
    ThisAIEntity.EntityAnimator.SetBool(walking, true);
    ThisAIEntity.EntityAgent.enabled = true;
    ThisAIEntity.EntityAgent.speed = ThisAIEntity.GetMovementSpeed() * roamingTemplate.MovementSpeedModifier;
    ThisAIEntity.EntityAgent.acceleration = ThisAIEntity.EntityAgent.speed;
    ThisAIEntity.EntityAgent.updateRotation = true;
    MovementStateBlendCompleted = false;
    NextRoamPositionCheck = 0;
    GetNewRoamingPoint();

    spawnedPosition = ThisAIEntity.transform.position; // Store the spawned position
}
#

Lastly, Add a new private variable in the class to store the spawned position:

#

modify the AIStateRoaming class:

grave stratus
#

Thanks I have this problem

chilly wind
#

The code is not fully tested and I am still tweaking it a bit, so share if you have improvements ๐Ÿ™‚ Thanks

lyric citrus
#

I'll definitely try this out ๐Ÿ™‚

chilly wind
#

Note: Experiment for your own use and if you improve it, please share ๐Ÿ™‚

#

It is not final yet!

#

Obviously backup your originals first.

raven tundra
#

๐Ÿ’ช โค๏ธ POGGIES

raven tundra
#

K i imported and it dos work, sometime the NPC glitches out and gets just set back to the spawn transform 0 again but it dos fixes the problem, only dos look bit off is like the have a blink and get teleported

#

@chilly wind

#

here a video showing them being teleported

#

dont play with sound its not me talking but just watch the stag teleport

#

anyway maybe i am setting it up wrong

#

in the AI roaming or do you guys have the same problem

chilly wind
#

For now it does that yes, but I may add code to check how far the player is and if the player cannot see the AI, just destroy the AI and if the player can see the AI, add code to get a new point instead of moving it to spawnpoint perhaps? We shall see. It currently is supposed to move stuck AI back to spawnpoint yes, which is not ideal ofcourse...but like I said WIP ๐Ÿ™‚

chilly wind
#

@raven tundra Try this instead: ```public override AIState Execute()
{
CombatEntity newTarget = ThisAIEntity.SearchTarget(roamingTemplate.viewDistance, roamingTemplate.viewAngle, roamingTemplate.AutoAggroDistance);
if (newTarget != null)
{
ThisAIEntity.ThisCombatEntity.SetTarget(newTarget);
return ThisAIEntity.GetChaseState();
}

if (ThisAIEntity.IsPlayerInteractionState()) return this;
HandleRoaming();

float distanceToTarget = Vector3.Distance(ThisAIEntity.transform.position, RoamTargetPosition);
if (!IsRoamingPaused && distanceToTarget > roamingTemplate.RoamPointThreshold && distanceToTarget > roamingTemplate.MinDistanceToMove)
{
    ThisAIEntity.MoveAgent(RoamTargetPosition);
}
else
{
    ThisAIEntity.ResetMovement();
}

timeSinceLastPositionCheck += Time.deltaTime;
if (timeSinceLastPositionCheck >= 15f)
{
    float distanceMoved = Vector3.Distance(previousPosition, ThisAIEntity.transform.position);
    if (distanceMoved < 3f)
    {
        Debug.Log("AI boar has moved less than 3 meters in the last 15 seconds. Finding a new valid point and making the AI walk there.");
        GetNewRoamingPoint();
    }
    else
    {
        previousPosition = ThisAIEntity.transform.position;
    }
    timeSinceLastPositionCheck = 0f;
}

return this;

}

#

With this updated Execute() method, the AI will find a new valid point and make the AI walk there instead of resetting it to the spawned position when it hasn't moved more than 3 meters in the last 15 seconds.

#

(Just edit the previous AIStateRoaming.cs)

chilly wind
#

Here are the most current versions: V0.26

grave stratus
#

Does it stop the walk in place and prevent the teleport we see above? ๐Ÿ˜„

chilly wind
#

yes

#

so far it does for all my tests...

#

the 'teleport' version was the old version, where it moved back to spawn point, but now it tries to find a valid navmesh point. I tested successfully on deers and boars and medium steep terrain so far ๐Ÿ™‚

grave stratus
#

Awesome thanks! I will give it a try tomorrow am ๐Ÿ™‚

chilly wind
#

Sure thing, let me know if you discover any exceptions please.

grave stratus
#

Will do ๐Ÿ™‚

cursive linden
#

Hello. I changed the scripts and the NPCs won't move. What could be the reason for this?

cursive linden
#

MissingReferenceException: The object of type 'AIStateIdle' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.

chilly wind
#

Those errors might be caused by incorrect settings in your NPC, navmesh not baked etc. The scripts works fine when setup correctly. Suggest you ask for support in the support channel and learn from the vids in the demo scene first. ๐Ÿ™‚

lost blaze
#

looks like ai just died or despawned right before it was gonna switch a state, not rlly that much of an issues since it sorts itself out

#

as for moving, you need to have navmesh baked
spawner set to spawn on ground layer
terrain set to ground layer
need the right behavior on it (one with roaming)

#

here's a msg with things to check for #rpgb-chat message

cursive linden
#

Everything worked fine on the original download. I've baked and reset the NavMesh many times, so I'm pretty sure it wasn't a mistake. Until I inserted these two new scripts, the NPCs moved through the terrain without error.

chilly wind
#

Keep in mind the default is to be stuck for 15 seconds. Feel free to experiment and list specific errors you get and provide screenshots or a small gif perhaps, and I will try to help in my free time. I am constantly tweaking my own scripts, so I am sure We have not found all the bugs yet.

#

you can test to reduce it to 5 if you want to see if that helps timeSinceLastPositionCheck += Time.deltaTime; if (timeSinceLastPositionCheck >= 5f) { float distanceMoved = Vector3.Distance(previousPosition, ThisAIEntity.transform.position); if (distanceMoved < 3f) { Debug.Log("AI has moved less than 3 meters in the last 15 seconds. Finding a new valid point and making the AI walk there."); GetNewRoamingPoint(); } else { previousPosition = ThisAIEntity.transform.position; } timeSinceLastPositionCheck = 0f; }

#

so change 15 to 5 seconds

#

Also, Your Terrain needs to be set to 'Ground' and you have to spawn NPC's on Ground and or Obstacle layers. I am sure you know this already, but just incase...

cursive linden
#

Yes, I already know the basics, so this is a completely new bug for me. Even though I reinstalled the original script, the error message is the same. Even when I re-imported the RPG - builder to eliminate all possible errors.

smoky vigilBOT
#

@cursive linden just reached level 2!

chilly wind
#

Try to describe what you do under community-support rather, as others are more likely to offer advice there perhaps

#

and of course you must actually delete the originals or move them to a backup folder.

cursive linden
#

This is the original c-sharp script which is unchanged. It can be seen that it works well, as the NPCs move. After I replace it with the script provided here, they just stand still and get the error message mentioned above. He wrote that the code might need to be changed, as already shown above. This is exactly in which script. I'm sorry, but I'm very new to programming, so I don't know.

cursive linden
#

Well this time without error, it works very well! I appreciate your help!!!

chilly wind
raven tundra
#

๐Ÿ’ช

static junco
#

this is awesome, thanks @chilly wind
added the new scripts and they work great except for one thing - now half of my spawners won't spawn at all. (strange that it's only some, not all of them.) anyone else have this issue?

chilly wind
#

Hmm, I did not change anything with spawners, but check that they are Ground,Obstacle and that your terrain is set to ground and re-bake navmesh perhaps, then check that spawner cylinder actually intersects that.

static junco
#

thanks!! will do

static junco
#

I checked out all the possibilities - it wasn't the spawner settings, they work properly after reverting to the default RPGB AI Roaming scripts. The new Roaming scripts seem to causing certain of my AI types to just not spawn for some reason, regardless of the Behavior template. makes no sense at all, and isn't throwing any errors, but I will keep digging ๐Ÿšง

chilly wind
#

Also make sure it is endless and a large enough distance and not set to limited whike testing. You can change it later to limited of course.

chilly wind
#

Paste an example of one of your spawners here, just incase

wintry latch
chilly wind
#

Excellent, glad to hear it. I won't say its perfect yet, so if you improve upon it, share please ๐Ÿ™‚

wintry latch
#

My only chance to improve anything is to use ChatGPT sadly ๐Ÿ™‚ I am working on it tho. Thank you for sharing โค๏ธ

chilly wind
#

You are very welcome.

stuck basalt
#

can confirm that it also fixes my issue with the ai walking in place. Thank you!

plain umbra
#

Amazing work thanks for the updated files

grave stratus
#

Its safe to use the fix? ๐Ÿ™‚

lost blaze
#

Tho i think thomas may have modified it slightly but yes should be fine as long as you're using the latest version of snake's scripts