Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

A* pathfinding project 2D top-down movement

Discussion in 'Scripting' started by Stonewood1612, Jan 17, 2015.

  1. Stonewood1612

    Stonewood1612

    Joined:
    Sep 15, 2014
    Posts:
    32
    Hey folks.

    So in my latest project I'm working on a 2D top-down shooter, and I'm currently working on getting my spawned enemies to move towards a (still) target. I'm using Aron's A* pathfinding project, and I have set everything up for it to work with 2D mode. The grid graph is functional, and my script (which is currently pretty much the same as the example getting started script on the documentation for the project) allows the enemy to find a path. Until that point it works, but my enemies don't move just yet.

    In nearly all the examples they use a character controller to move the object. But I can't have one, since I'm using 2D colliders and rigidbodies. And the character controller is made for 3D games of course.

    So the line in the example script that I can't use is:

    controller.SimpleMove (dir);

    Which is what makes the object move towards a calculated waypoint on the path.
    So do I need to use the rigidbody2D or transform.translate instead?
     
  2. stulleman

    stulleman

    Joined:
    Jun 5, 2013
    Posts:
    44
    I would recommend using
    Code (CSharp):
    1. rigidbody2d.velocity = dir * maxSpeed;
    or something like this.
     
  3. Stonewood1612

    Stonewood1612

    Joined:
    Sep 15, 2014
    Posts:
    32
    With some changes here and there that works perfectly. Thank you.