- Home /
How to Move an NPC from anyplace to a specific place in 2D?
Hello, I'm making a 2D game where the player controls many characters by giving commands with the mouse. The scene is a cross section of a space ship (with multiple floors with stairs connecting them) with various character sprites on board. The spaceship moves around the screen, the sprites with it, of course. It doesn't move a lot, mostly bobs around and changes pitch.
I want to be able to command crew by clicking buttons on the HUD. For Example, if the player clicks on a button that says "Abandon Ship", then I want the characters to walk from wherever they are standing to the escape pod which will always be in the same place, relative to the ship.
I am able to find the Characters positions relative to the ship, but I can't figure out how to get them there. If one is on the 3rd level of the ship and the escape pod is on the first level - I can't get him to walk to the first set of stairs, descend, then to the next set of stairs, descend and then walk to the pod. See picture for more details
I'm an experience programmer but new to C#, Unity and graphical programming. Any help would be most appreciated. Thanks so much
RayJr
Unity has in built "path finding", I have never used it but it may do what you need for this.
Hi, I looked into that a bit but could only see it in 3d, as in a large flat space with obsticles.
Answer by HarshadK · Oct 20, 2014 at 01:21 PM
One method is to use the waypoints. You can create waypoints in your game map like one waypoint at the start of the ladder and then second at the end of the ladder. You can add as many waypoints as required. Now you can store these waypoints in an array or List.
Now move your NPC from it current position to the closest waypoint on its floor then from there you can move it using another closest waypoint to the current waypoint and so on till you reach your pod. You can simply use any movement methods like MoveTowards.
Remember, more the number of waypoints will allow you to have more accurate movement but will require more computation. You can decide the waypoints as per your need.
Ah, that sounds like what i'm looking for. Are Waypoints an actual object/parameter, or do you code them from scratch sort of thing ?
Thanks so much :)
You can place empty objects at the place where you want your waypoints to be. Set tags of these game objects to be something like WayPoints. Now you can get positions of all those game objects which you can then use to move your NPC.
Or you can write your waypoint positions in your script also. But this will make changes difficult each time you move an object related to the waypoint or something.
Better go with the first method.
Here is my problem which is that I want to make my lower cube (enemy) use gravity and mass, and my cube never moves in straight down/Up in y-axis it just looks that cube is jumping down or jumping up on horizontal walls and follow the duck (Player). I am using "A* pathfinding". The image is for just understanding. Ins$$anonymous$$d of using the red path the cube following the green path as shown in the image. I am very thankful in advance for helping me.