// Update is called once per frame void Update() { //Look at the player Vector3 locationOfPlayer = playerTransform.position; //locationOfPlayer.y = locationOfPlayer.y - 3f; // this offset is to prevent the enemies from targetting your head. //// now they target a bit lower and stay on the floor Vector3 lookTarget = new Vector3(locationOfPlayer.x, this.transform.position.y, locationOfPlayer.z); transform.LookAt(lookTarget); //Calculate 2D distance to player float dX = transform.position.x - playerTransform.position.x; float dZ = transform.position.z - playerTransform.position.z; float distance = Mathf.Sqrt(dX * dX + dZ * dZ); if (distance <= attackingDistance && isMoving) // person is within range of the attack. stop moving { anim.SetBool("Moving", false); isMoving = false; isAttacking = true; anim.SetTrigger("Attack1Trigger"); } if (isAttacking && distance >= attackingDistance) // person is out of range of the attack. keep moving. { anim.ResetTrigger("Attack1Trigger"); isAttacking = false; timeLeft = 0.8f; } //If the distance is smaller than the walkingDistance //condition:distance < walkingDistance && distance >= attackingDistance + 1f else if (distance >= attackingDistance + 1f) { //Move the enemy towards the player with smoothdamp //transform.position = Vector3.SmoothDamp(transform.position, player.position, ref smoothVelocity, smoothTime); if (!isMoving) { anim.SetBool("Moving", true); isMoving = true; } } if (isAttacking) { timeLeft -= Time.deltaTime; } if (isAttacking && timeLeft < 0) { playerBehaviour.takeDamage(10); timeLeft = 0.8f; if (playerBehaviour.getHealth() <= 0) { Debug.Log("DEAD"); GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy"); /*foreach (enemy e in enemies) * { * e.stopAnimating(); * }*/ spawnScript.startStop(); } } }