Using RevertFromBackup to update walkability with a moving character

I had issues with the GraphUpdateScene never updating any nodes on the graph that I was currently working with, so I decided to implement the code below. This seems to produce the results I need, but this is through the use of RevertFromBackup to make sure the only nodes that are marked unwalkable are the ones currently occupied by the object. Is there an issue with using this method, or is there a better alternative available?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pathfinding;
namespace FSS.BOW
{
	[RequireComponent(typeof(Collider))]
    public class ActivePathObstacle : MonoBehaviour
    {
        [SerializeField] private float m_updateRate = .1f;
        [SerializeField] private float m_boundsMultiplier = 2f;
        private Collider m_col;
        private float m_lastTime;
        private GraphUpdateObject m_guo;
        private void Awake()
        {
            m_col = GetComponent<Collider>();
            m_lastTime = Time.time;
        }
        private void Start()
        {
            Bounds bounds = m_col.bounds;
            bounds.Expand(m_boundsMultiplier);

            m_guo = new GraphUpdateObject(bounds);
            m_guo.modifyWalkability = true;
            m_guo.setWalkability = false;
            m_guo.trackChangedNodes = true;
            m_guo.updatePhysics = false;
        }
        void Update()
        {
            if (m_lastTime + m_updateRate <= Time.time)
            {
                Bounds bounds = m_col.bounds;
                bounds.Expand(m_boundsMultiplier);
                m_guo.bounds = bounds;
                AstarPath.active.UpdateGraphs(m_guo);
                m_guo.RevertFromBackup();

                m_lastTime = Time.time;
            }
        }
    }
}

Hi

I generally do not recommend updating the graph around a moving agent, usually some kind of local avoidance is better for that.

In any case, if you definitely need this. Isn’t it more reasonable to just add the character to the collision testing layer mask?
The DynamicGridObstacle class implements all logic necessary for regular moving obstacles. Check out the Example2 example scene for an example of how they can be used.

1 Like