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:
- NPC starts at default location X,Y
- NPC searches for a target T based on some factor (e.g. the florist searches for the flower stand)
- NPC moves to T using only X and Y movement (no diagonal movement)
- 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:
private void Update()
{
direction = player.transform.position - transform.position;
direction = Vector3.Normalize(direction);
velocity = direction * speed;
rb.MovePosition(rb.position + velocity * Time.deltaTime);
}
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!