public override void BehaviourUpdate() { //Profiler.BeginSample("BehaviourUpdate"); #region debug if (Input.GetKeyDown(KeyCode.B)) { combat.Block(currentStance.blocks[0]); } if (Input.GetKeyDown(KeyCode.N)) { combat.B_BlockFlinch(); } if (Input.GetKeyDown(KeyCode.M)) { combat.Evade(currentStance.evades[1]); } if (Input.GetKeyDown(KeyCode.L)) { combat.MeleeAttack(null, currentStance.attacks[1]); } #endregion //cache some variables for later nearestEnemy = sensing.nearestEnemy; currentStance = unitAI.currentStance; myTransformPosition = entity.myTransform.position; enemyIsNull = nearestEnemy == null; //1. look at the enemy, if no enemy present -> continue looking in the movement direction if (!enemyIsNull) { //but only if something chenged if (nearestEnemyLastFrame != nearestEnemy) { nearestEnemysTransform = nearestEnemy.myTransform; movement.LookAt(nearestEnemysTransform); } nearestEnemyPosition = nearestEnemysTransform.position; currentSquaredDistanceToNearestEnemy = (nearestEnemyPosition - myTransformPosition).sqrMagnitude; } else { if (nearestEnemyLastFrame != nearestEnemy) { movement.StopLookAt(); } } switch (meleeAttackBehaviourState) { //2. if theres an enemy -> walk towards him case MeleeAttackBehaviourState.Idle: if (!enemyIsNull) { movement.MoveTo(nearestEnemyPosition); meleeAttackBehaviourState = MeleeAttackBehaviourState.MovingToNearestEnemy; } break; case MeleeAttackBehaviourState.MovingToNearestEnemy: //check if the enemy is still alive if (enemyIsNull) { meleeAttackBehaviourState = MeleeAttackBehaviourState.Idle; movement.Stop(); } //else check if we reached the destination else if (currentSquaredDistanceToNearestEnemy < rangeToEnterMeleeCombatSquared) { meleeAttackBehaviourState = MeleeAttackBehaviourState.InMeleeFight; } //if not, check if we need to update the destination else { if (Time.time > nextUpdateDestinationTime) { nextUpdateDestinationTime = Time.time + updateDestinationInterval; movement.MoveTo(nearestEnemyPosition); } } break; case MeleeAttackBehaviourState.InMeleeFight: //check if the enemy is still alive if (enemyIsNull) { meleeAttackBehaviourState = MeleeAttackBehaviourState.Idle; movement.Stop(); } //check if the enemy has not escaped else if (currentSquaredDistanceToNearestEnemy > rangeToEnterMeleeCombatSquared) { if (combat.currentCombatState == CombatState.CombatIdle) { movement.MoveTo(nearestEnemyPosition); meleeAttackBehaviourState = MeleeAttackBehaviourState.MovingToNearestEnemy; } } //else check if i am in range for an attack - fix range and attack else { //decision making .- thats the tricky part here if (combat.currentCombatState == CombatState.CombatIdle) { //check if we should block because our adversary was faster if (nearestEnemy.combat.currentCombatState == CombatState.PreparingMeleeAttack) { //if he is lucky he will block or evade, if not then not :D if (Random.Range(0, 2) == 0) { if (Random.Range(0, 2) == 0) { combat.Block(currentStance.blocks[0]); } else { combat.Evade(currentStance.evades[0]); } } else { combat.MeleeAttack(nearestEnemy, currentStance.attacks[Random.Range(0, currentStance.attacks.Length)]); } } else { combat.MeleeAttack(nearestEnemy, currentStance.attacks[Random.Range(0, currentStance.attacks.Length)]); } } else if (combat.currentCombatState == CombatState.Blocking) { //check if we can stop blocking and attack if (nearestEnemy.combat.currentCombatState != CombatState.PreparingMeleeAttack) { combat.MeleeAttack(nearestEnemy, currentStance.attacks[Random.Range(0, currentStance.attacks.Length)]); } } } break; } nearestEnemyLastFrame = nearestEnemy; // Profiler.EndSample(); }
void Update() { float horizontalInput = Input.GetAxis("Horizontal"); float verticalInput = Input.GetAxis("Vertical"); if (unitIsPosessedByPlayer) { if (movement.agent.desiredVelocity.sqrMagnitude > 0) { movement.RotateTo(movement.agent.desiredVelocity, Time.deltaTime); } #region movement if (horizontalInput != 0 || verticalInput != 0) { Vector3 flatUpVectorOfCamera = Camera.main.transform.up; flatUpVectorOfCamera.y = 0; flatUpVectorOfCamera.Normalize(); Vector3 flatRightVectorOfCamera = Camera.main.transform.right; flatRightVectorOfCamera.y = 0; flatRightVectorOfCamera.Normalize(); flatRightVectorOfCamera *= horizontalInput; flatUpVectorOfCamera *= verticalInput; Vector3 movementVector = flatRightVectorOfCamera + flatUpVectorOfCamera; if (combat.currentCombatState == CombatState.CombatIdle) { movement.MoveTo(playerEntity.transform.position + movementVector); } } else { // movement.Stop(); } #endregion #region combat if (Input.GetMouseButtonDown(1)) { combat.Block(unitAI.currentStance.blocks[0]); } else if (!Input.GetMouseButton(1)) { if (Input.GetKeyDown(KeyCode.G)) { combat.MeleeAttack(null, unitAI.currentStance.attacks[1]); } if (Input.GetMouseButtonDown(0)) { if (combat.currentCombatState == CombatState.Blocking || combat.currentCombatState == CombatState.CombatIdle || combat.currentCombatState == CombatState.PreparingBlock) { Debug.Log("hor: " + horizontalInput); Debug.Log("vert: " + verticalInput); if (horizontalInput != 0 || verticalInput != 0) { Debug.Log("yes"); combat.MeleeAttack(null, unitAI.currentStance.attacks[1]); } else { combat.MeleeAttack(null, unitAI.currentStance.attacks[0]); } } } else if (Input.GetKeyDown(KeyCode.E)) { combat.MeleeAttack(null, unitAI.currentStance.attacks[2]); } else if (combat.currentCombatState == CombatState.Blocking || combat.currentCombatState == CombatState.PreparingBlock) { combat.StopBlocking(); } } if (Input.GetKeyDown(KeyCode.Space)) { if (horizontalInput != 0 || verticalInput != 0) { Debug.Log("yep"); Vector3 inputDirection = new Vector3(horizontalInput, 0f, verticalInput); combat.Evade(unitAI.currentStance.evades[0]); } } #endregion } else { //simple rts camera movement Vector3 flatUpVectorOfCamera = Camera.main.transform.up; flatUpVectorOfCamera.y = 0; flatUpVectorOfCamera.Normalize(); Vector3 flatRightVectorOfCamera = Camera.main.transform.right; flatRightVectorOfCamera.y = 0; flatRightVectorOfCamera.Normalize(); flatRightVectorOfCamera *= horizontalInput; flatUpVectorOfCamera *= verticalInput; Vector3 movementVector = flatRightVectorOfCamera + flatUpVectorOfCamera; cam.transform.position += movementVector; } if (Input.GetAxis("Mouse ScrollWheel") != 0) { cam.GetComponent <Camera>().orthographicSize -= Input.GetAxis("Mouse ScrollWheel") * 2; } if (Input.GetKeyDown(KeyCode.P)) { if (unitIsPosessedByPlayer) { Deposess(); } else { Posess(); } } }