Forum rules - please read before posting.

Making the Player run automatically if reaching a distant Hotspot/walkTarget

Hi there!

I believe I probably already asked months ago, but can't find the answer anymore.

I was having a look at PlayerInteraction.cs, deciding if I could extend it by overriding a few methods, but I guess there's a simpler, less intrusive way to do it.

Simply put, on a touchscreen I'd love to:

  • Make the player automatically run if the interaction point with a Hotspot requires walking a path that is longer than a threshold value.
  • Make the player automatically run if clicking on the floor results in having to wait too much for the player to reach it. So, as above, if the distance (path) is longer than a threshold value (the same value or another), the player automatically runs towards it.

I'm pretty sure AC is designed in a way that allows me to get this behaviour via scripting, without having to mess with the source code too much.

how would you hook into these events, in a safe way? (eg: in some scenes you can't run, or you can't walk, and I want to avoid breaking these rules)

Comments

  • Should be possible with the OnCharacterSetPath event, coupled with a check for GetHotspotMovingTo():

    using UnityEngine;
    using AC;
    
    public class AutoRun : MonoBehaviour
    {
    
        public float hotspotRunThreshold = 4f;
        public float regularRunThreshold = 4f;
    
        void OnEnable ()
        {
            AC.EventManager.OnCharacterSetPath += OnSetPath;
        }
    
        void OnDisable ()
        {
            AC.EventManager.OnCharacterSetPath -= OnSetPath;
        }
    
        void OnSetPath (Char character, Paths path)
        {
            if (character.IsPlayer && character.IsPathfinding ())
            {
                float distance = Vector3.Distance (character.transform.position, path.Destination);
    
                Hotspot movingToHotspot = KickStarter.playerInteraction.GetHotspotMovingTo ();
                if ((movingToHotspot != null && distance > hotspotRunThreshold) ||
                    (movingToHotspot == null && distance > regularRunThreshold))
                {
                    character.isRunning = true;
                }
            }
        }
    
    }
    
  • edited April 2020

    Thanks Chris!

    I made a few changes, hope this makes sense and didn't forget any corner case scenario.

    Maybe I could skip calculating LOS distance, but you never know.

    protected virtual void OnSetPath(Char character, Paths paths)
    {
        if (!character.IsPlayer || !character.IsPathfinding()) return;
        if (!KickStarter.stateHandler.IsInGameplay() || KickStarter.stateHandler.IsInCutscene()) return;
        if (KickStarter.playerInput.runLock == PlayerMoveLock.AlwaysWalk) return;
    
        var _dist = Vector3.Distance(character.transform.position, paths.Destination);
        var _paths = character.GetPath();
    
        if (paths)
        {
            var _pathDist = paths.GetTotalLength();
            if (_pathDist > _dist)
                _dist = _pathDist;
        }
    
        var movingToHotspot = KickStarter.playerInteraction.GetHotspotMovingTo();
        if (movingToHotspot && _dist > hotspotRunThreshold ||
            !movingToHotspot && _dist > regularRunThreshold)
            character.isRunning = true;
    }
    
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.