unable to attack enemy objects?
I've been working in the 2d roguelike tutorial, and after finishing it, I'm going through and adding some more features to it to make it my own thing, and learn more about coding.
I'm trying to add a feature to allow the player to attack the enemy characters, which is set up the same as both of the functions for attacking walls, and enemies attacking the player, but when the character moves towards an enemy space, he doesn't attack at all.
The only difference is that I've set up an item that the player must collect on each level before he can attack enemies, which is just a boolean, called "hasAxe" I've set to public temporarily so I can verify that it's triggering correctly in the inspector.
Does anyone know why this is failing? there's no runtime error in the console.
attack function in the player script:
if(component.GetType() == typeof(Enemy))
{
if (hasAxe == false)
{
return;
}
else if (hasAxe == true)
{
//Declare hitEnemy and set it to equal the encountered component.
Enemy hitEnemy = component as Enemy;
//Call the EnemyHit function of hitEnemy passing it axeDamage, the amount of healthpoints to be subtracted.
hitEnemy.LoseHealth(axeDamage);
//play the chop sound
SoundManager.instance.RandomizeSfx (chopSound1, chopSound2);
//Play the axe animation
animator.SetTrigger ("playerAxe");
}
}
Enemy Damage code:
public void LoseHealth (int loss)
{
//Subtract lost food points from the players total.
enemyHealth -= loss;
CheckForDeath();
}
private void CheckForDeath()
{
if (enemyHealth <=0)
{
gameObject.SetActive (false);
}
}
at this point, the code gets a little long to feel comfortable posting all of it to display on a webpage, so I'm going to throw the scripts in pastebin.
Player Script: http://pastebin.com/ABKd18vC
Enemy Script: http://pastebin.com/zeDXVHu4
Wall Script: http://pastebin.com/PNXkvAbw
MovingObject(parent of Player and Enemy): http://pastebin.com/mXR8U7uX
Just a quick comment. You're only calling the attack logic in OnCant$$anonymous$$ove<T>.
I think this may be your problem as the original logic seems to be implemented to attack certain walls, so it may not even be called when attempting to attack and enemy.
Sorry, but I'm confused.. I'm fairly new to coding. Where else would I need to call it? I have the function set up in Enemy the same way it should be for Player and Wall
public void LoseHealth (int loss)
{
//Subtract lost food points from the players total.
enemyHealth -= loss;
CheckForDeath();
}
Bump.. Can anyone help, please? I'm stuck at a standstill until I can figure this out :(
Answer by DGH1988 · Aug 24, 2017 at 07:55 AM
Hi there,
I am struggling with a similar issue. I tried copying as much of the system used in the wall script but can't get it to work. What I did was assign a second script to the Enemy1 and 2 prefab which is basically a copy of the wallscript. Everything in the player script and the copy of the wall script (assigned to the enemy1 and 2) of course has unique names usually just by replacing the word "wall" with "enemy". I also tried adding the concept used in wall script to the enemy script but that also didn't work. Have you found a solution yet? Of course I can elaborate further if requested.
Cheers,
Daan
Boom! I got it!
I replaced the wall script with a script that contains both the wall and the enemy being damaged and removed, of course amended the player script as well a little bit.