Character main script:
switch (actionMode)
{
case RobotActionMode.Defend:
mode = "defend";
// Defend Idle animation finish, resume other animation
if (Time.time > defendModeTime+defendTimer)
{
animator.SetTrigger("Defend Idle End");
defendTimer = Time.time;
actionMode = RobotActionMode.Targeting;
}
break;
case RobotActionMode.Targeting:
mode = "target";
target = UpdateEnemyRange();
t = target;
if (enemyInAttackRange.Count != 0)
actionMode = RobotActionMode.RotateToTarget;
else
actionMode = RobotActionMode.PathFinding;
break;
case RobotActionMode.RotateToTarget:
mode = "rotate";
RotateToTarget(target.position);
if (TargetDirectionCheck(target))
actionMode = RobotActionMode.Attacking;
break;
case RobotActionMode.Attacking:
mode = "attack";
// pick the closest enemy that LOS and direction OK
// select highest damage weapon
weaponSelected = WeaponSelector();
// fire weapon
if (weaponSelected != 99)
{
weapon[weaponSelected].coolDownCounter = Time.time;
// Make sure the target is in standby mode, and also forbid it to switch to other mode when melee
if (weapon[weaponSelected].type == "Melee")
{
if (target.GetComponent<RobotAI>().actionMode != RobotActionMode.Attacking)
{
// target.GetComponent<RobotAI>().actionMode = RobotActionMode.Defend;
actionMode = RobotActionMode.Waiting;
WeaponFire();
}
}
else
{
actionMode = RobotActionMode.Waiting;
WeaponFire();
}
}
else
actionMode = RobotActionMode.Targeting;
break;
case RobotActionMode.Waiting: // Waiting Weapon Effect Finish
mode = "waiting";
break;
The only function that can change RobotActionMode to defend, that is also inside the character main script:
public void Defend()
{
// defend cool down ok, robot is not using weapon now, not already in defend mode
if (Time.time > defendCoolDown + defendTimer && actionMode!=RobotActionMode.Waiting && actionMode!= RobotActionMode.Attacking && actionMode!=RobotActionMode.Defend)
{
animator.SetBool("Walking", false);
if (shieldL == true)
animator.SetTrigger("Left Shield Defend");
else if(shieldR == true)
animator.SetTrigger("Right Shield Defend");
else
animator.SetTrigger("Hands Defend");
Debug.Log(mode + " -> defend");
actionMode = RobotActionMode.Defend;
defendTimer = Time.time;
}
}
Other objects call following scripts in their own fixedupdate:
void DefendSignalSend()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, sensorOffset) && hit.transform.GetComponent<RobotAI>())
hit.transform.GetComponent<RobotAI>().Defend();
}
I can see in the debug log, RobotActionMode change from waiting to defend. Why the if condition in Defend() is ignored?