Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i achive the following?
.Create points around player.
.In these points enemies will fill them, 1 point 1 enemy and so on.
.The rest of the enemies should go idle and wait.
.If 1 enemy dies then the idle enemy closest to the point should get and fill in the empty point.

What I have tried:

Also lets say i have two armys on the scene.
20 versus 20.
I want the NPC's to attack eatch other and not go after the player unless the player attacks them.
Thank you.
Posted
Updated 26-May-22 6:59am
Comments
[no name] 26-May-22 13:48pm    
Objects in 2D and 3D space usually have coordinates (x,y,z). You determine who is "around" something be calculating the distances between objects. Objects are "near" or "far" based on their "range".
The Other John Ingram 26-May-22 16:45pm    
The whole fun is to kill the bad guys, if the bad guys kill each other what fun is that. Are you going to keep track on the dead bodies of the bad guys.
Half the fun is looting the bad guys you just killed. I am not going to mention walls or (locked) doors, oops my bad.
i would keep the bad guys in a idle mode until the good guys get near or attack them.
good luck
simple world 27-May-22 0:19am    
I know about the coorinates, but i am trying to do that with an array and a foreach loop and i can figure how to get the position of many enemies.
LOOK AT THE CODE BELLOW
playerPos = player.transform.position;
foreach (GameObject enemy in enemies)
{
enemyPos = enemy.transform.position;
distanceBetweenPlayerAndEnemies = Vector3.Distance(playerPos, enemyPos);
if(distanceBetweenPlayerAndEnemies <= agent.stoppingDistance)
{
Debug.Log("Enemy has arrived");
enemy.GetComponent<navmeshagent>().isStopped = true;
}
}


As for why i want them to kill each other it is basically a strategy game where you stand back tell your troops to do something but everything will be in 3D, and if you decide to join the battle, i dont want you to be overwhelmed with the amount of enemies attacking you.
Thank you.
[no name] 27-May-22 13:28pm    
"Get the position of many enemies". You create a (temporary) "list" or dictionary or "array" of the enemies in question with anything else applicable to that "frame" (e.g. distance); adding them from your "for loop" for each new frame.
simple world 29-May-22 7:47am    
I figured out how to do that this is the code:
for (int i = 0; i < enemies.Length; i++)
{
enemyPos = enemies[i].transform.position;

for(int j = 0; j < playerPoints.Length; j++)
{
distanceBetweenPlayerAndEnemies = Vector3.Distance(playerPoints[j].transform.position, enemyPos);
if (distanceBetweenPlayerAndEnemies <= agent.stoppingDistance)
{
enemies[i].transform.position = playerPoints[j].transform.position;
i++;
}


}
}

PlayerPoints are just some empty game objects around the player, and the enemies just go there where they are close.
But now i have a new question, as soon as 1 enemy is close to the player then all teleport on those positions no matter how far, i tried to make it and if statement like if they are far then dont teleport them but it doesnt work.
Any ideas?
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900