void AttackEnemy(GameObject enemy1)
{
float xDiff = enemy1.transform.position.x - transform.position.x;
float yDiff = enemy1.transform.position.y - transform.position.y;
float zval = transform.position.z;
Vector3 attackangle = new Vector3 (xDiff, yDiff, zval);
// animate
Debug.Log("AttackEnemy");
if (Vector3.Angle(Vector3.left, attackangle) >= 0 && Vector3.Angle(Vector3.left, attackangle) <= 45)
{
Debug.Log("AttackEnemy");
animator.Play("HALeft2");
}
else if (Vector3.Angle(Vector3.right, attackangle) >= 0 && Vector3.Angle(Vector3.right, attackangle) <= 45)
{
animator.Play("HARight2");
}
else if (Vector3.Angle(Vector3.up, attackangle) >= 0 && Vector3.Angle(Vector3.up, attackangle) < 45)
{
animator.Play("HAUp2");
}
else if (Vector3.Angle(Vector3.down, attackangle) >= 0 && Vector3.Angle(Vector3.down, attackangle) < 45)
{
animator.Play("HADown2");
}
// wait and only run this code that increments damage_taken once every N frames (N = 2 for now)
// try to actually understand the math
GameObject attackgo = new GameObject();
GameObject defendgo = new GameObject();
GameObject damagego = new GameObject();
attack_skill_hits attack = attackgo.AddComponent<attack_skill_hits>();
attack_skill_evades defend = defendgo.AddComponent<attack_skill_evades>();
swinging_thrusting damage = damagego.AddComponent<swinging_thrusting>();
if (Time.time > nextActionTime && InAttackRange(transform.position, 50))//do animations separately? - yes
{
if (attack.skill_hits(7) && !defend.skill_evades(5, 7, 9))
{
if (swinging)
{
damage_taken += damage.dmg_swinging(15) + 2;//+ weapon damage modifier + other modifiers(weapon specialization etc.);
}
else
{
damage_taken += damage.dmg_thrusting(15) + 1;// + weapon damage modifier + other modifiers(weapon specialization etc.);
}
Debug.Log(damage_taken);
}
nextActionTime = Time.time + period;
}
}
currently this code plays every frame that the state is in attackenemy, so the game objects attackgo, defendgo, and damagego are created every frame. the only alternative is to put the game object creation code outside any functions, but that generates an error (which still lets the game run, or at least did once, but i think itâs a problem). if i put the generation code in start or awake i get a âdoesnât exist in the current contextâ error for attackgo, defendgo, and damagego respectively.
is it such a big deal to create new game objects like crazy (they show up in the editor), or is there some other alternative?
edit: could i just delete some of the objects so that the game isnât inundated with them? very messy approach
edit2: hereâs the solution someone gave me - https://hastebin.com/ilupoqodan.cs