← Back to all addons

NPC Idle Walk (3.*) 🚢🚢

Posted by Mr.Titan on Apr 28, 2025 at 5:12 PM

ExperimentalFeature3.0 Verified
πŸ’‘ 3

Makes the npcs walk around a radius. Steps in the comments

Simple Implementation (Base Speed)

This approach it’s used by both implementations, the difference it that it uses the character speed. Personally i thing it looks weird, because the character it’s not “crusing” in a chilling way.

Change the AIController.cs:

Add parameters


        [Header("Idle Movement Settings")]
        [SerializeField] private bool enableIdleMovement = true;
        [SerializeField, Min(0.1f)] private float idleRadius = 2.0f;
        [SerializeField, Min(0.1f)] private float idleMovementInterval = 3.0f;

Add variables


        private Vector2 idleTargetPosition;
        private float idleTimer;
        private bool m_isIdleMoving = true;

Add Method:

private Vector2 GetIdlePosition()
{
    float soughtDistance =
        m_target ?
        m_soughtDistanceFromTarget :
        m_soughtDistanceFromMasterTarget;
    //Has a master OR Don't idle OR It just stopped chasing
    if (m_master || 
        !enableIdleMovement ||
        !m_isIdleMoving && Vector2.Distance(m_homePosition,transform.position) > soughtDistance)
    {
        return m_homePosition;
    }

    idleTimer -= Time.deltaTime;

    if (idleTimer <= 0.0f)
    {
        idleTargetPosition = m_homePosition + UnityEngine.Random.insideUnitCircle * idleRadius;
        idleTimer = idleMovementInterval;
        m_isIdleMoving = true;
    }
    return idleTargetPosition;
}

Changes to UpdateTargetPosition

Update the “else” content: From:

m_targetPosition = m_homePosition;

To:

m_targetPosition = GetIdlePosition();

Change the id statement of the method OnFixedUpdate From:

if (Vector2.Distance(transform.position, m_targetPosition) > soughtDistance

To:

if (
m_isIdleMoving ?
Vector2.Distance(transform.position, m_targetPosition) > 0.5f
: Vector2.Distance(transform.position, m_targetPosition) > soughtDistance
)

Custom Speed for Idle

Make sure to follow the last approach first, before doing this. It includes the possibility to change the speed of the idle walk.

Add Paramter:

        [SerializeField, Min(0.1f)] private float idleMovementSpeed = 1.0f;`

In the method OnFixedUpdate, the id state statement you changed ealier, you should replace the following inside

From:


                m_lerpedTargetDirection =
                    !m_subject.IsMoving() ?
                    m_steeringAverageOutput :
                    Vector2.Lerp(m_lerpedTargetDirection, m_steeringAverageOutput, Time.fixedDeltaTime * m_steeringDriftResponsiveness);

                m_subject.SetMovementDirection(m_lerpedTargetDirection.normalized);

To:

m_lerpedTargetDirection =
    !m_subject.IsMoving() ?
    m_steeringAverageOutput :
    Vector2.Lerp(m_lerpedTargetDirection, m_steeringAverageOutput, Time.fixedDeltaTime *
    m_steeringDriftResponsiveness);

m_subject.SetMovementDirection(m_lerpedTargetDirection.normalized * (m_isIdleMoving ? idleMovementSpeed/10:1f)); // /10 for normalization in setup params

Here’s the complete file

NPC Idle Walk (3.*) 🚢🚢

πŸ“œ Scripts

πŸ’¬ Comments (0)

Be the first to comment! Join our Discord to share your thoughts.

Want to continue the conversation?