- Home /
Enemy detection while pathfinding through the map
Hi guys, im creating a MOBA game like League of Legends. I'm almost done but there is one final thing to be made. I have enemies going from my base to the enemy base with multiple waypoints to get there. In what I need help, through the path that the enemies are running to get to the enemy base there are multiple turrets.
I want the enemies to:
When a turret is near, they stop going to the next waypoint
Move to the turret that is near them
Stop at a certain distance of the turret
Play an animation of attack (not necessary)
When the turret dies the enemy goes to the next waypoint (The turret is already taking damage when the minion collides with the turret)
Repeat this whenever a turret is near
I got to demonstrate the game tomorrow to my class and teachers, so any help is welcome.
Working Enemy Waypoint Script:
var waypoint : Transform[];
var speed : float = 20;
var currentWaypoint : int;
// Declaração de variáveis
function Update()
{
if(currentWaypoint < waypoint.length)
{
var target : Vector3 = waypoint[currentWaypoint].position;
// Saber onde está o caminho a percorrer
var moveDirection : Vector3 = target - transform.position;
var velocity = rigidbody.velocity;
// Rodar para o nosso caminho com determinada velocidade
if(moveDirection.magnitude < 1)
// Saber o valor absoluto do número
{
currentWaypoint++;
}
else
{
velocity = moveDirection.normalized * speed;
}
}
// Vai mover-se como na realidade e também poderá ser influenciado pela gravidade
rigidbody.velocity = velocity;
transform.LookAt(target);
}
Script that im working with waypoints and enemy detection:
var waypoint : Transform[];
var speed : float = 20;
var currentWaypoint : int;
var Damage = 15;
var maxDistance = 15;
// Declaração de variáveis
function Update()
{
if(currentWaypoint < waypoint.length)
{
var target : Vector3 = waypoint[currentWaypoint].position;
// Saber onde está o caminho a percorrer
var moveDirection : Vector3 = target - transform.position;
var velocity = rigidbody.velocity;
var nearestDistanceSqr = Mathf.Infinity;
var taggedGameObjects = GameObject.FindGameObjectsWithTag("MinionR");
var nearestObj : Transform = null;
// Loop that verifies who is the nearest enemy
for (var obj : GameObject in taggedGameObjects)
{
var objectPos = obj.transform.position;
var distanceSqr = (objectPos - transform.position).sqrMagnitude;
if (distanceSqr < nearestDistanceSqr)
{
nearestObj = obj.transform;
nearestDistanceSqr = distanceSqr;
}
}
//Mecanismo de Disparo
var distance = (nearestObj.transform.position - transform.position).magnitude;
if(distance < maxDistance && Time.time > TimeToFireAt)
{
Fire(); //Criar disparo e efeitos
TimeToFireAt = Time.time + FireRate; //Set Time
}
// Rodar para o nosso caminho com determinada velocidade
if(moveDirection.magnitude < 1)
// Saber o valor absoluto do número
{
currentWaypoint++;
}
else
{
velocity = moveDirection.normalized * speed;
}
}
// Vai mover-se como na realidade e também poderá ser influenciado pela gravidade
rigidbody.velocity = velocity;
transform.LookAt(target);
}
Regards, ExyloN
Answer by Bovine · Jul 18, 2012 at 07:56 PM
Good luck, I'm afraid if you still have a lot of that list of things to do, you have a long, somewhat impossible night ahead.
What you're talking about is rudimentary AI. We have a tile based game and although simple, we ended up writing our own Behaviour Trees.
I would highly recommend this approach and it is possibly the quickest and cleanest approach there is, but you cannot write a BT in one night. You could, however, use the Behave library, but I don't know if the AngryAnt site is still running, so support may be tricky.
If you use the Behave library, I'd say there's a slim chance you could integrate and have it doing some or all of the above before morning.
Behave is a free behaviour tree library and can be found in the asset store.
Looks like Angry Ant is up and running still - it was not there the other day:
There are some videos on Emil's site here:
http://angryant.com/wp-content/uploads/2010/09/Behave-starting-from-scratch.mov
http://angryant.com/wp-content/uploads/2010/10/FromTreeToCode.mov
AiGameDev provides some excellent resources for this kind of thing:
http://aigamedev.com/insider/article/behavior-trees/ http://aigamedev.com/premium/masterclass/unity-behave/
Not all of it is free I am afraid :(
it's a big topic, it might be too much to take in and integrate in one night, but it's probably the most robust and potentially the quickest and most flexible way to go.
Otherwise, it's time to hack!!
If you're going with Behave, avoid decorators at all cost, they likely will NOT work the way that you think they do. I don't even think they work the way Emil says they do in the AiGameDev video!
Your answer
Follow this Question
Related Questions
enemy waypoints and detection scipting 2 Answers
Enemy Not Facing target direction (waypoint) correctly when moving between waypoints 1 Answer
Waypoint System help 1 Answer
Obstacle avoidance not working 0 Answers
Enemy units behave normally, until they rotate, then their AI seems to break 0 Answers