- Home /
enemy need to stop and shoot while walking on waypoint
I made a tower defence game, eveyrhting works perfect. The enemies are following waypoints, using hotween.
I want to make some enemies to attack towers, instead of just pass away. For this; i need a break while they follow waypoints, a code for detection of towers. I see that in the tower scripts, towers use bool void check range. How can i implement same system on enemies? This is the enemy waypoint script(not complete, but i guess this is the important part);
IEnumerator OnSpawn()
{
//wait one frame so Start() gets called before OnSpawn() on a new instantiation
//(we need to set "myID" before calling it below)
yield return new WaitForEndOfFrame();
//get Vector3 array with waypoint positions
waypoints = pathContainer.waypoints;
//get waypoint array of Vector3
InitWaypoints();
//start movement
StartMove();
}
//initialize waypoint positions
internal void InitWaypoints()
{
//recreate array used for waypoint positions
wpPos = new Vector3[waypoints.Length];
//fill array with original positions and add custom height
for (int i = 0; i < wpPos.Length; i++)
{
wpPos[i] = waypoints[i].position + new Vector3(0, sizeToAdd, 0);
}
}
//starts movement
public void StartMove()
{
//we set this gameobject position directly to the first waypoint and
//we also add a defined size to our object height,
//so our gameobject could "stand" on top of the path.
transform.position = wpPos[0];
//we're now at the first waypoint position,
//so directly call the next waypoint
CreateTween();
}
//creates a new HOTween tween which moves us to the next waypoint
//(defined by passed arguments)
internal void CreateTween()
{
//prepare HOTween's parameters, you can look them up here
//http://www.holoville.com/hotween/documentation.html
////////////////////////////////////////////////////////////
//create new HOTween plugin for curved paths
//pass in array of Vector3 waypoint positions, relative = true
plugPath = new PlugVector3Path(wpPos, true, pathtype);
//orients the tween target along the path
//constrains this game object on one axis
if (orientToPath)
plugPath.OrientToPath();
//create TweenParms for storing HOTween's parameters
tParms = new TweenParms();
//sets the path plugin as tween position property
tParms.Prop("position", plugPath);
tParms.AutoKill(true);
//use speed based tween with linear easing
tParms.SpeedBased();
tParms.Ease(EaseType.Linear);
tParms.OnComplete(OnPathComplete);
//create a new tween, move this gameobject with given arguments
tween = HOTween.To(transform, maxSpeed, tParms);
}
Should i add public void? Or if state?
If they found a tower, they must stop and attack. There are many attacking script examples, this part is ok. But If tower hitpoint become zero/ they need to continue, but i dont how can i do this.
Thank you for your help.
I think you should look up ENU$$anonymous$$S, which hold different states of actions like say a patrol state or a EngageTarget state.
Also you should run a check like this to make sure you dont attack a dead tower: if(tower.health>0){ } This way you only attack a tower that isn't dead.
Your answer
![](https://koobas.hobune.stream/wayback/20220613200915im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Get First Object In List 2 Answers
Enemy AI problems 2 Answers
rigidbody enemy goes only forward, no damage delay. 2 Answers
2D Enemy AI issues... 1 Answer