Skip to content

Follow + Random Walk

Can I have a creature walking randomly nearby the PC but start following him as soon as the PC starts moving or gets distant? And then if the PC stops the creature goes back to walking randomly? I've been trying to accomplish this using Heartbeat scripts but it's not really working. Any ideas?

Comments

  • dunahandunahan Member Posts: 139
    OnHeartbeat can be possible, but I would use some other. For example OnPerception and use a pseudo heartbeat....
    You should check the position of the PC, with a saved position from him. If it has changed, clear all actions and follow/move to the new position, or if not walk randomly. At last, redo this check and action.
    Yet, I'm not at the toolset, so I have no script handy...
  • gpeddinogpeddino Member Posts: 51
    That sounds pretty interesting, the OnPerception idea. I'll do some experimenting. Thanks for that!
  • gpeddinogpeddino Member Posts: 51
    edited March 2020
    Thought I'd share what I came up with... I ended up using only a heartbeat script but it worked beautifully! It could probably be tweaked and improved though.

    The creature in question is a Lantern Archon that will follow the PC around to provide light. My next step is creating my own RandomWalk function to have more control, as I'm willing to incorporate this system to the henchman in my module.

    const int FOLLOW = 0;
    const int WANDER = 1;
    
    void SetFollowMode(int iMode, object oPC)
    {
        SetLocalInt(OBJECT_SELF, "FollowMode", iMode);
    
        ClearAllActions();
        switch (iMode)
        {
            case FOLLOW:
            ActionForceFollowObject(oPC, 2.0f);
            break;
    
            case WANDER:
            ActionRandomWalk();
            break;
        }
    }
    
    int GetFollowMode()
    {
        int iMode = GetLocalInt(OBJECT_SELF, "FollowMode");
    
        return iMode;
    }
    
    void main()
    {
        object oPC = GetFirstPC();
    
        /// Get current PC position.
        vector vPC1 = GetPosition(oPC);
    
        /// Get stored PC position.
        float vPCx = GetLocalFloat(OBJECT_SELF, "vPC.x");
        float vPCy = GetLocalFloat(OBJECT_SELF, "vPC.y");
        float vPCz = GetLocalFloat(OBJECT_SELF, "vPC.z");
        vector vPC2 = Vector(vPCx, vPCy, vPCz);
    
        /// Are they the same?
        if (vPC1 == vPC2)
        {
            if (GetFollowMode() != WANDER)
            {
                SetFollowMode(WANDER, oPC);
            }
        }
        else
        {
            if (GetDistanceBetween(OBJECT_SELF, oPC) > 5.0)
            {
                if (GetFollowMode() != FOLLOW)
                {
                    SetFollowMode(FOLLOW, oPC);
                }
            }
        }
    
        /// Store new position.
        SetLocalFloat(OBJECT_SELF, "vPC.x", vPC1.x);
        SetLocalFloat(OBJECT_SELF, "vPC.y", vPC1.y);
        SetLocalFloat(OBJECT_SELF, "vPC.z", vPC1.z);
    }
    
Sign In or Register to comment.