- Home /
Enemy Damage to Player
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<NavMeshAgent> ();
InitAnimation ();
}
//Initialize Animations
void InitAnimation()
{
bearAnimation = GetComponent<Animation>();
AnimationEvent attackEvent = new AnimationEvent ();
attackEvent.time = attackImpact;
attackEvent.functionName = "Impact";
attackAnimation.AddEvent (attackEvent);
}
void Impact()
{
player.GetComponent<Player>().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<Animation>();
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<Enemy>().EnemyGetHit (damage);
}
}
if(!playerAnimation.IsPlaying (attackAnimation.name))
{
isAttacking = false;
}
}
public void PlayerGetHit(int enemyDamage)
{
health = health - enemyDamage;
}
}
Hope someone help me, Thanks in Advance!
Your answer
Follow this Question
Related Questions
Player Damage to enemy damage melee system 0 Answers
Dungeon Enemy Help 1 Answer
Player attacks once but the enemy takes double damages! 1 Answer
Question on Player Damage 1 Answer
auto select enemys 3 Answers