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

Enemy Damage to Player

$
0
0
Hi Everyone! I'm new in scripting, and I have this problem, I set my enemy's damage to 10 and when the enemy attack the player it reduce the players health to 10 but the problem here is when I created/duplicated 2 another enemies it reduces the health of the player by the total damage of 3 of them even though only 1 enemy is attacking my player. this is mya Enemy Script: public class Enemy : MonoBehaviour { public int health; public int damage; public float range; public float chasingRange; public float attackRange; public Transform player; public float attackImpact; NavMeshAgent navAgent; Animation bearAnimation; public AnimationClip idleAnimation; public AnimationClip runAnimation; public AnimationClip attackAnimation; // Use this for initialization void Start () { navAgent = GetComponent (); InitAnimation (); } //Initialize Animations void InitAnimation() { bearAnimation = GetComponent(); AnimationEvent attackEvent = new AnimationEvent (); attackEvent.time = attackImpact; attackEvent.functionName = "Impact"; attackAnimation.AddEvent (attackEvent); } void Impact() { player.GetComponent().PlayerGetHit(damage); } // Update is called once per frame void Update () { if(!IsInRange()) { Chase (); FollowPlayer (); } else { navAgent.SetDestination (transform.position); bearAnimation.CrossFade (idleAnimation.name); } AttackMethod (); } void OnMouseOver() { Player.opponent = transform; } bool IsInRange() { if (Vector3.Distance (transform.position , player.position ) < chasingRange) { return true; } else { return false; } } void Chase() { navAgent.SetDestination (player.transform.position); bearAnimation.CrossFade (runAnimation.name); } void FollowPlayer() { if (Vector3.Distance (transform.position , player.position ) < range) { navAgent.SetDestination (player.transform.position); bearAnimation.CrossFade (runAnimation.name); } else { navAgent.SetDestination (transform.position); bearAnimation.CrossFade (idleAnimation.name); } } public void EnemyGetHit (int playerDamage) { health = health - playerDamage; } void AttackMethod() { if(Vector3.Distance (transform.position, player.position) < attackRange) { transform.LookAt (player.position); bearAnimation.CrossFade (attackAnimation.name); } } } And here is my player Script public class Player : MonoBehaviour { public int health; public int damage; public float range; public static bool isAttacking; public static Transform opponent; Animation playerAnimation; public AnimationClip attackAnimation; // Use this for initialization void Start () { playerAnimation = GetComponent(); isAttacking = false; } // Update is called once per frame void Update () { Attack (); } void Attack () { if(Input.GetMouseButtonUp (0)) { if(opponent != null && Vector3.Distance(opponent.position, transform.position) < range) { isAttacking = true; transform.LookAt (opponent.position); playerAnimation.CrossFade (attackAnimation.name); opponent.GetComponent().EnemyGetHit (damage); } } if(!playerAnimation.IsPlaying (attackAnimation.name)) { isAttacking = false; } } public void PlayerGetHit(int enemyDamage) { health = health - enemyDamage; } } Hope someone help me, Thanks in Advance!

Viewing all articles
Browse latest Browse all 1488

Trending Articles



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