Search Unity

Urgent! Tower Defense Game Nav Mesh agent problem

Discussion in 'Navigation' started by Clementhui, Sep 23, 2018.

  1. Clementhui

    Clementhui

    Joined:
    Sep 23, 2018
    Posts:
    1
    I am making a tower defense game and i come to the problem when coming to path finding.
    I made roadblocks to be placed on the field to block the path and the enemies are nav mesh agents but i don't want the player to completely block off the road. I created a script to destroy any block if it block every way to the end. It was successful but i can place the block that block the way but not other blocks after i blocked the way. My script for the nodes to place block

    GameObject turretToBuild = buildManager.instance.GetTurretToBuild();
    turret = (GameObject)Instantiate(turretToBuild, transform.position + offset, transform.rotation);
    Debug.Log("Object Instansiated");
    if (turret.transform.GetChildCount() != 0)
    {
    Renderer renderer = turret.transform.FindChild("range").gameObject.GetComponent<Renderer>();
    renderer.enabled = false;
    }
    Agent.calPath();
    if (Agent.pathValid() == false)
    {
    Debug.Log("Can't Build Here");
    Destroy(turret);
    return;
    }

    Scripts for the agents

    public static bool pathValid()
    {

    NavMesh.CalculatePath(itself.position,tran.position,NavMesh.AllAreas,path);
    if(path.status == NavMeshPathStatus.PathPartial)
    {
    Debug.Log("path imcomplete");
    return false;
    }
    else {
    return true;
    }

    }

    I don't know why it happened. Please answer thank you
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Before building the block, simulate placing it(make an invisible one), try qnd ge a path from the start, if you can, place the block, if not tell the player he cant build there