Search Unity

Pacman game using A* algorithm ?

Discussion in 'Scripting' started by diahnovianti, Aug 30, 2019.

  1. diahnovianti

    diahnovianti

    Joined:
    May 15, 2019
    Posts:
    4
    Hello I'm newbie here.. I make game like pacman, but the ghost are stoped moving when crash with the obstacle, I using A* algorithm for pathfinding. I'm very flustrated right now..(I not using navmesh) Can you help me,, this is the code:

    void movementNPC (List<Node> path)
    {
    PathSize = path.Count;
    speed = 0.1f;

    if (path.Count - 1 != 0 && nod.walkable)
    {
    for (int i = 1; i < path.Count - 1; i++)
    {
    Vector3 directionNew = new Vector3(path[i - 1].worldPosition.x, transform.position.y, path[i - 1].worldPosition.z);
    if (path.Count != null)
    transform.position = Vector3.MoveTowards(transform.position, directionNew, Time.deltaTime * speed);

    //rotate
    Vector3 targetDir = (directionNew - transform.position).normalized;
    Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, speed * Time.deltaTime, 0.0f);
    transform.rotation = Quaternion.LookRotation(newDir);


    }
    }

    what's worng with my code?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    First of all please format your code. See the first post in the forum.

    Second of all, answer standard debugging questions:

    1. what should the code do (be explicit)
    2. what is it doing (be explicit, including error messages)
    3. what have you tried to fix it
    4. where do yo think the problem might be?

    Draw pictures, include screencaps, show off a video, whatever.