Help with custom Local Avoidance movement script + TopDownEngine

Hi everyone, I came here to ask for your help. With my team we are working in a 2D top down game and we are using MoreMountains TopDownEngine for that TopDownEngine official page.

Where is our problem? Well, with the movement system that TopDownEngine implements. Basically the plugin uses a “MovementDirection” to move the characters (player and enemies), so to make the typical AI that follows the player using the A* Pathfinding Project we calculate the direction between the enemy position and the next node to reach.

        public virtual void MoveToDestination()
        {
            if (_path == null)
            {
                return;
            }

            if (_currentWaypoint >= _path.vectorPath.Count && !_reachedEndOfPath)
            {
                _reachedEndOfPath = true;

                if (OnMove != null)
                {
                    OnMove();
                }

                StartPath();
                _currentWaypoint = 0;
                return;
            }
            else
            {
                _reachedEndOfPath = false;
            }

            Vector2 newDirection = (_path.vectorPath[_currentWaypoint] - OriginPoint).normalized;
            float distance = (OriginPoint - _path.vectorPath[_currentWaypoint]).sqrMagnitude;

            _characterMovement.SetMovement(newDirection);

            if (_characterController.IsForcingToLookAtObject)
            {
                _characterController.StopForcingToLookAtObject();
            }

            if (distance < _nextWaypointSquaredDistance)
            {
                _currentWaypoint++;
            }
        }

But now we need to implement the Local Avoidance to prevent enemies from accumulating at a single point when they follow the player, but we don’t know how to obtain the direction and give it to the CharacterMovement componen as we did on the previous function.

 Vector2 newDirection = (_path.vectorPath[_currentWaypoint] - OriginPoint).normalized;
 _characterMovement.SetMovement(newDirection);

The idea is not to break the TopDownEngine movement logic, is there any way to obtain the same direction but using Local Avoidance?

Hey,
Usually we recommend using RVO for local avoidance, (the build in movement scripts handle this without a problem).

Depending on how your AI works there are many different solutions you can approach. A manager could find various positions for the group of Agents. Depending on distance, weapon type etc…

Alternatively depending on what graph type you’re using you could make something custom using the ITraversableProvide and NNConstraint. I’ve written a bit more detailed answer here: Avoidance, but not local

Hi

Does the TopDownEngine not support any kind of speed adjustment? It is possible to get the info you want from the local avoidance system, but it really needs to be able to set the speed of the agent every frame, otherwise the avoidance will not work well.

1 Like

Yes, the CharacterMovement component has a MovementSpeed property to modify the current character speed.

Well. There is some example code for how to integrate the local avoidance system with a custom movement script here: https://arongranberg.com/astar/docs/rvocontroller.html However it gives you back a delta for how much it wants to move during this frame.

Mmm, I’ll try a

 Vector2 newDirection = ((OriginPoint + delta) - OriginPoint).normalized;
 _characterMovement.SetMovement(newDirection);

That will work… sort of. It will remove a lot of the nuances in the local avoidance and I think it will be very hard for it to stop at any point.