private void FixedUpdate() { magic1 = enemyMagic.ToString(); // activate movement animation animator.SetFloat("Speed", Mathf.Abs(HorizontalMove) * speed); /* this part check with linecast, if the enemy reach the end of a plattform or if something else (not enemy or player) * block his way. If yes, flipp. */ Vector2 lineCastPos = (Vector2)trans.position - (Vector2)trans.right * widh; bool isGrounded = Physics2D.Linecast(lineCastPos, lineCastPos + Vector2.down, enemyMask); bool isBlock = Physics2D.Linecast(lineCastPos, lineCastPos - (Vector2)trans.right * 0.05f, enemyMask); /* * this is yout smoothMovement part, i just add it here */ Vector3 targetVelocity = new Vector2((Time.fixedDeltaTime * speed) * 10f, rgb.velocity.y); rgb.velocity = Vector3.SmoothDamp(rgb.velocity, targetVelocity, ref velocity, MovementSmoothing); if (!isGrounded || isBlock) { flip(); } /* * Movement speed */ Vector2 myVel = rgb.velocity; myVel.x = trans.right.x * speed; rgb.velocity = myVel; /* * check if child is archer or not (important for the arrows creating part) */ if (IsArcher != 0) { attacker = this.GetComponent <NPCArcherAttack>(); } /* * Attack cooldown part: time is attack cooldown value. If play time is bigger then enemy can attack one time, after this this time cooldown value * has to change intern, because times go forward, so the next time flag has to suits the new situation. But 1.2s after attack, the attack- * animation stop. I suppost this is the animation time. Of cause animation attack stop also, when the attack flag is default */ if (attackingF) { //enemy stop move by attack horizontalMove = 0; EnemySpeed = 0; if (Time.time > time) { //only for archer important. I dont like it, but it was late, when i create this if (IsArcher != 0) { attacker.Attack(transform.position.x, transform.position.y, -transform.rotation.y); } animator.SetBool("Attack", attackingF); /*cooldown has to go with new time * Example: attackcooldown 3s, time starts with 0s * when time is 3 -> attack. Next attack has to be in time 6s -> time+cooldown = 6s */ time = Time.time + timerA; } if (Time.time > (time - timerA + 1.2f)) { animator.SetBool("Attack", false); } } else if (!attackingF) { animator.SetBool("Attack", false); } }