Create-a-Game-Project
Create-a-Game-Project copied to clipboard
Episode 7 of Unity Create a Game Series
I was pretty sure I didn't leave anything out when following the tutorial, but for some reason when I get to the part where you input the damage and health system the enemy seems to kill me in one hit. No matter what my health is, when I start the game it does the normal attacking animation, but then one shots me. The damage and health system works fine when I hit the enemy, but for some reason not the other way around. What I have so far...
`[RequireComponent (typeof (UnityEngine.AI.NavMeshAgent))] public class Enemy : LivingEntity {
public enum State {Idle, Chasing, Attacking};
State currentState;
UnityEngine.AI.NavMeshAgent pathfinder;
Transform target;
LivingEntity targetEntity;
Material skinMaterial;
Color originalColor;
float attackDistanceThreshold = .5f;
float timeBetweenAttacks = 1;
float damage = 1;
float nextAttackTime;
float myCollisionRadius;
float targetCollisionRadius;
bool hasTarget;
protected override void Start()
{
base.Start();
pathfinder = GetComponent<UnityEngine.AI.NavMeshAgent>();
skinMaterial = GetComponent<Renderer>().material;
originalColor = skinMaterial.color;
if (GameObject.FindGameObjectWithTag("Player") != null)
{
currentState = State.Chasing;
hasTarget = true;
target = GameObject.FindGameObjectWithTag("Player").transform;
targetEntity = target.GetComponent<LivingEntity>();
targetEntity.OnDeath += OnTargetDeath;
myCollisionRadius = GetComponent<CapsuleCollider>().radius;
targetCollisionRadius = target.GetComponent<CapsuleCollider>().radius;
StartCoroutine(UpdatePath());
}
}
void OnTargetDeath()
{
hasTarget = false;
currentState = State.Idle;
}
void Update () {
if (hasTarget)
{
if (Time.time > nextAttackTime)
{
float sqrDstToTarget = (target.position - transform.position).sqrMagnitude;
if (sqrDstToTarget < Mathf.Pow(attackDistanceThreshold + myCollisionRadius + targetCollisionRadius, 2))
{
nextAttackTime = Time.time + timeBetweenAttacks;
StartCoroutine(Attack());
}
}
}
}
IEnumerator Attack()
{
currentState = State.Attacking;
pathfinder.enabled = false;
Vector3 originalPosition = transform.position;
Vector3 dirToTarget = (target.position - transform.position).normalized;
Vector3 attackPosition = target.position - dirToTarget * (myCollisionRadius);
float attackSpeed = 3;
float percent = 0;
skinMaterial.color = Color.red;
bool hasAppliedDamage = false;
while (percent <= 1)
{
if (percent >= .5f && !hasAppliedDamage) {
hasAppliedDamage = true;
targetEntity.TakeDamage(damage);
}
percent += Time.deltaTime * attackSpeed;
float interpolation = (-Mathf.Pow(percent, 2) + percent) * 4;
transform.position = Vector3.Lerp(originalPosition, attackPosition, interpolation);
yield return null;
}
skinMaterial.color = originalColor;
currentState = State.Chasing;
pathfinder.enabled = true;
}
IEnumerator UpdatePath() {
float refreshRate = .25f;
while (hasTarget) {
if (currentState == State.Chasing)
{
Vector3 dirToTarget = (target.position - transform.position).normalized;
Vector3 targetPosition = target.position - dirToTarget * (myCollisionRadius + targetCollisionRadius + attackDistanceThreshold/2);
if (!dead)
{
pathfinder.SetDestination(targetPosition);
}
}
yield return new WaitForSeconds(refreshRate);
}
}
} `