Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Enemy is not taking damage!

$
0
0
Hi guys, so I'm following a tutorial by GucioDevs and I have run into an error. When my player attacks, the enemy doesn't take damage. Yes, I have the player tagged as Enemy. Please help :))! Here is my code: using System.Collections; using System.Collections.Generic; using UnityEngine; public class TurretAI : MonoBehaviour { public int curHealth; public int maxHealth = 5; public float distance; public float wakeRange; public float shootInterval; public float bulletSpeed = 100; public float bulletTimer; public bool lookingRight = true; public bool awake = false; public GameObject bullet; public Transform target; public Animator anim; public Transform shootPointLeft, shootPointRight; void Awake() { anim = gameObject.GetComponent(); } void Start() { curHealth = maxHealth; } void Update() { anim.SetBool("Awake", awake); anim.SetBool("LookingRight", lookingRight); RangeCheck(); if (target.transform.position.x > transform.position.x) { lookingRight = true; } if (target.transform.position.x < transform.position.x) { lookingRight = false; } if (curHealth <= 0) { Destroy(gameObject); } } void RangeCheck() { distance = Vector3.Distance(transform.position, target.transform.position); if (distance < wakeRange) { awake = true; } if (distance > wakeRange) { awake = false; } } public void Attack(bool attackingRight) { bulletTimer += Time.deltaTime; if (bulletTimer >= shootInterval) { Vector2 direction = target.transform.position - transform.position; direction.Normalize(); if (!attackingRight) { GameObject bulletClone; bulletClone = Instantiate(bullet, shootPointLeft.transform.position, shootPointLeft.transform.rotation) as GameObject; bulletClone.GetComponent().velocity = direction * bulletSpeed; bulletTimer = 0; } if (attackingRight) { GameObject bulletClone; bulletClone = Instantiate(bullet, shootPointRight.transform.position, shootPointRight.transform.rotation) as GameObject; bulletClone.GetComponent().velocity = direction * bulletSpeed; bulletTimer = 0; } } } public void Damage (int damage) { curHealth -= damage; gameObject.GetComponent().Play("TinyEnemyReact"); } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>