Search Unity

Question Need help with Top Down NPC movement

Discussion in '2D' started by PaperMouseGames, May 27, 2020.

  1. PaperMouseGames

    PaperMouseGames

    Joined:
    Jul 31, 2018
    Posts:
    434
    Hi there! I'm working on a top down RPG and I wanted to have NPCs in my game's town that move around and do stuff, but I'm having trouble finding any good ways of doing this.

    So here is the break down of what I want:

    1. NPC starts at default location X,Y
    2. NPC searches for a target T based on some factor (e.g. the florist searches for the flower stand)
    3. NPC moves to T using only X and Y movement (no diagonal movement)
    4. NPC plays animation, then moves on to next target

    So, the only step giving me trouble is step 3. I wrote up a simple follow player script in a few minutes as I experiment with this:

    Code (CSharp):
    1. private void Update()
    2.     {
    3.         direction = player.transform.position - transform.position;
    4.         direction = Vector3.Normalize(direction);
    5.         velocity = direction * speed;
    6.         rb.MovePosition(rb.position + velocity * Time.deltaTime);
    7.     }
    The issue is that I want to remove diagonal movement for ground based NPCs. The other issue is that the NPCs can get stuck on stuff with colliders. I don't mind this a ton for enemies (many games do this, like Zelda Link to the Past), but for the town NPCs I really want them to not get stuck around the town.

    I thought that maybe rather than using a target system I should try using a waypoint system where I determine exactly the walking path of each Town NPC. So for the florist, I would actually put in the waypoints path from the spawn point to the flower stand, then play the animation, then follow another waypoint path.

    The waypoints seem like it would be a lot of work though, so I wanted some options on this before I start doing that. I wondered if there was a viable way of making NPCs "look out" for obstacles using raycasts but I guess I'm not sure what I would have them do when they do find an obstacle.

    Anyway, thanks for the help!
     
  2. Racines

    Racines

    Joined:
    Jan 20, 2014
    Posts:
    35
    I think the best way to resolve this problem is to use A* algorithm.
    You will be able to specify target to your NPC and the A* algorithm will find the shortest path to it, avoiding obstacles.

    Here some good YoutubeVideo on the subject:

     
  3. PaperMouseGames

    PaperMouseGames

    Joined:
    Jul 31, 2018
    Posts:
    434
    @Racines

    Thanks! This looks great, and correct me if I'm wrong, but it looks like the free version is ok to use in commercial games?

    I'd like to see if there's a way to disable the diagonal pathing on certain enemies though, but other than that this might be the best solution.
     
  4. Racines

    Racines

    Joined:
    Jan 20, 2014
    Posts:
    35
    Yes the free version seems to be ok with using it in commercial games.
     
  5. PuppyPolice

    PuppyPolice

    Joined:
    Oct 27, 2017
    Posts:
    116
    Yeah it is possible to disable diagonal movement for the free a* but I think you gonna need two grids then one for diagonal and one for none diagonal calculations.