This is my first time asking a question here at StackOverflow.
I have finished the Unity 3D official "Roguelike 2D" tutorial, and trying to expand on it on my own. When the Player
tries to walk into a wall, he damages it instead, eventually breaking through.
I have removed sounds and animator commands for clarity.
protected override void OnCantMove<T> (T component)
{
Wall hitWall = component as Wall;
hitWall.DamageWall (wallDamage);
}
And when the Enemy
tries to walk into the Player
, it damages him instead, hurting him.
protected override void OnCantMove <T> (T component)
{
Player hitPlayer = component as Player;
hitPlayer.LoseFood (playerDamage);
}
playerDamage
is set by the Enemy script, and wallDamage
is set by the Player script (meaning both are set by the one doing the damage.) LoseFood
and DamageWall
are set by the Player and the Wall scripts (meaning both are set by the one receiving the damage.)
I want the Enemy
to be able to damage Walls, like the Player
is. So, I tried changing the Enemy script's OnCantMove
part to something like this:
protected override void OnCantMove<T> (T component)
{
Wall hitWall = component as Wall;
hitWall.DamageWall (wallDamage);
Player hitPlayer = component as Player;
hitPlayer.LoseFood (playerDamage);
}
and this:
protected override void OnCantMove<T> (T component)
{
Wall hitWall = component as Wall;
Player hitPlayer = component as Player;
if (hitWall) {hitWall.DamageWall (wallDamage);}
else if (hitPlayer) {hitPlayer.LoseFood (playerDamage);}
}
Neither worked the way I wanted them to do. I even tried to replace the OnCantMove
function of the Enemy
to be the same as when the Player
hits the walls, but that only removed the ability the Enemy
had to hit the Player
, without enabling it to hit walls. What should I try next, to see if I can solve this problem?
If I manage to get this to work, I'm hoping I can continue to expand on this game in other ways as well, but I am very new at this. Thank you for any help and advice.