use a function twice
I'm working in the roguelike tutorial, and trying to edit the code and game to make it my own, and I'm having a little trouble.
I have a script called MovingObject as a parent of my player and enemy scripts.
I have a function in there called OnCantMove
protected abstract void OnCantMove <T> (T component)
where T : Component;
I have it again in player, but I need to use it twice. once for attacking enemies, and once for breaking walls. Does anyone know how to do this?
protected override void OnCantMove <T> (T component)
{
//Set hitWall to equal the component passed in as a parameter.
Wall hitWall = component as Wall;
//Call the DamageWall function of the Wall we are hitting.
hitWall.DamageWall (wallDamage);
if (hasAxe == false)
{
//Set the attack trigger of the player's animation controller in order to play the player's attack animation.
animator.SetTrigger ("playerChop");
}
else if (hasAxe == true)
{
//Set the attack trigger of the player's animation controller in order to play the player's attack animation.
animator.SetTrigger ("playerAxe");
}
}
attack enemy:
protected override void OnCantMove <T> (T component)
{
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 foodpoints to be subtracted.
hitEnemy.EnemyHit (axeDamage);
//play the chop sound
SoundManager.instance.RandomizeSfx (chopSound1, chopSound2);
//Play the axe animation
animator.SetTrigger ("playerAxe");
}
}
Answer by Ckboyd89 · Apr 06, 2016 at 11:44 PM
You can use the GetType() method to determine which component was passed. So it would look something like the following:
protected override void OnCantMove<T>(T component)
{
if(component.GetType() == typeof(Enemy))
{
Enemy hitEnemy = component as Enemy;
// Do enemy hit logic
}
else if(component.GetType() == typeof(Wall))
{
Wall hitWall = component as Wall;
// Do wall hit logic
}
}
Your answer
Follow this Question
Related Questions
It does not work removing items from the inventory 0 Answers
Rayscast from the GameObject that is not the player does not work 0 Answers
Error `UnityEngine.Component' does not contain a definition 1 Answer
[uNet] Multiple Spawnpoints with Selection 0 Answers
How to check if a player has passed a certain position?? 1 Answer