public override bool perform(GameObject agent) { Tank currentA = agent.GetComponent <Tank>(); AgentGOAP currentGOAP = agent.GetComponent <AgentGOAP>(); Debug.Log("Attack"); //Find a node if we don't have one and get the path to it if (targetNode == null) { //Finding a node in circle around the enemy tank var radius = 3; var vector2 = Random.insideUnitCircle.normalized * radius; var targetPos = currentA.EnemyTank.transform.position + new Vector3(vector2.x, 0, vector2.y); targetNode = currentA.CalculatePath(targetPos); Debug.Log(targetNode.transform.position); //If you can't see the enemy and we know his last position, search in the area around last know pos } else if (currentA.knownEnemyPosition != null && !currentA.canSeeEnemy && currentA.findNodeCloseToPosition(transform.position) == targetNode) { Debug.Log("Last position"); float randomX = Random.Range(-10, 10); float randomZ = Random.Range(-10, 10); Vector3 targetPos = currentA.knownEnemyPosition + new Vector3(randomX, 0, randomZ); targetNode = currentA.CalculatePath(targetPos); searchCount++; //If we don't have the last positon and can't see the enemy abort plan } else if (currentA.knownEnemyPosition == Vector3.zero && !currentA.canSeeEnemy) { return(false); } else if (currentA.findNodeCloseToPosition(transform.position) == targetNode) { reachedNode = true; } //If we have searched 5 locations abort plan if (searchCount > 5) { return(false); } //If health is under accepted level abort plan (bad variable name) return(currentA.healthOver10); }
//Walk towards node, choose node until enemy is seen public override bool perform(GameObject agent) { Tank currentA = agent.GetComponent <Tank>(); //Find a new node when we reached target node or when we don't have a target node if (targetNode == null || currentA.findNodeCloseToPosition(transform.position) == targetNode) { float randomX = Random.Range(minX, maxX); float randomZ = Random.Range(minZ, maxZ); Vector3 targetPos = new Vector3(randomX, 0, randomZ); targetNode = currentA.CalculatePath(targetPos); } if (currentA.canSeeEnemy) { enemySeen = true; } else { enemySeen = false; } return(true); }
public override bool perform(GameObject agent) { Tank currentA = agent.GetComponent <Tank>(); //Fidning a hiding spot, if we don't have one or if we can see the enemy tank if (targetNode == null || currentA.canSeeEnemy) { Vector3 targetPos = transform.position; bool isInView = true; RaycastHit hit; //Super not good but works while (isInView) { float randomX = Random.Range(minX, maxX); float randomZ = Random.Range(minZ, maxZ); targetPos = new Vector3(randomX, 0, randomZ); isInView = !Physics.Linecast(targetPos, currentA.EnemyTank.transform.position, out hit, ~(1 << gameObject.layer)); } Vector3 targetDir = targetPos - transform.position; Vector3 enemyDir = currentA.EnemyTank.transform.position - transform.position; //Check if the target node is towards the enemy tank if (Vector3.Dot(enemyDir, targetDir) < 0) { targetNode = currentA.CalculatePath(targetPos); } } else if (currentA.findNodeCloseToPosition(transform.position) == targetNode) { // nodeReached = true; } return(true); }