Character Follow AI

So i’m new to the whole coding thing and ive been learning little by little, and i was just wondering if there was a way to have one of my “Characters” somehow follow the Player. i already have where the Character can follow the Player but its only on one plane, if the Player jumps to another platform the Character just gets stuck and cant follow the player. how do i make the AI smart enough to jump and follow the Player through anything?

This is the code i have so far…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GhoulFollow : MonoBehaviour
{

    public float speed;
    public float stoppingDistance;

    private Transform target;

    // Start is called before the first frame update
    void Start()
    {
        target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
    }

    // Update is called once per frame
    void Update()
    {
        if(Vector2.Distance(transform.position, target.position) > stoppingDistance)
        {
            transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);

        }
    }
}

sorry i dont really know how to paste my code hope this is enough.

WELCOME!

Believe it or not, that’s actually a bit harder than you imagine. :slight_smile:

Some random ways, in rough order of difficulty going up:

  1. When the follower gets too far away, or simply just finds himself on a different platform from the player, he goes POOF and teleports to beside the player. Many production games use this as a primary way of following, or as a backup in case the follower gets stuck.

  2. When the player leaps from one platform and lands on another, something records the leap curve, then the follower runs to the same spot the player leaped from and blindly executes the same leap, just playing back the player’s sequence of positions.

  3. Hard-wire leap behaviors throughout your level: if the player is on platform X and the follower is on another platform, there would be code that tells the follower where to go and what sequence of jump / move to get up. You’d have to edit those and if the level changes, redo them, plus if the player moves on really quickly, the follower could get hopelessly behind.

  4. Put invisible markers in the level that are used to produce pathing, basically links between the platforms, and then implement a 2D pathing solution that the follower uses.

I would combine @Kurt-Dekker solutions if it were my game.
Create a script which takes 2 points, 1 for the jumping spot, and 1 for the landing - and calculate the distance needed to clear that jump.
Now you can basically hard-wire like Kurt said. But you really want to prevent doing it as it is time consuming in the long run.
I’d have the player create those 2 points whenever they jump. You can instantiate a prefab “Jumping Spot” when you jump, and a “Landing Spot” when you land.
If you want to prevent the NPC from jumping when the player just jumps around flat surface, you can have 2 raycasts, which checks if there’s a wall incoming, and if there’s a gap incoming, and flag out when neither conditions are true.
And, for safety measures, have the NPC teleport to the player in case they are stuck, again like Kurt said.

Good luck!

1 Like