Need enemy to attack player and deplete health
So I'm making a slender man sort of game and I have the enemy and the player, and have configured it to chase me when I get within 15 of it, and attack when it gets very close. I need to add a health bar in the UI then link it to my player's health. I want the player to be able to withstand 3 attacks.
Here's the code I have currently:
using UnityEngine;
using System.Collections;
public class chase : MonoBehaviour {
public Transform player;
static Animator anim;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update ()
{
Vector3 direction = player.position - this.transform.position;
float angle = Vector3.Angle(direction, this.transform.forward);
if (Vector3.Distance (player.position, this.transform.position) < 15 && angle < 40)
{
direction.y = 0;
this.transform.rotation = Quaternion.Slerp (this.transform.rotation,
Quaternion.LookRotation (direction), 0.1f);
anim.SetBool ("isIdle", false);
if (direction.magnitude > 1) {
this.transform.Translate (0, 0, 0.05f);
anim.SetBool ("isWalking", true);
anim.SetBool ("isAttacking", false);
} else {
anim.SetBool ("isAttacking", true);
anim.SetBool ("isWalking", false);
}
} else {
anim.SetBool ("isIdle", true);
anim.SetBool ("isWalking", false);
anim.SetBool ("isAttacking", false);
}
}
}
Any help is appreciated, even if it'll be too hard to make a health bar, that's not essential.
Thanks in advance :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612114421im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do i make a colision with a specific object 2 Answers
Asset Store Tools In the drop-down menu no tabs " paskage manager" 0 Answers
Unity Multiplayer: when i hit another player all players health bars go down 0 Answers
Abnormal and Complex healthbar Shapes. Unity 5 2 Answers
does someone have a player health script and a way to make a health bar? 0 Answers