I have 4 scripts two for the health of enemy and player and two for the damage of the same two. The player and enemy won’t collide ,they just walk through each other. Help me please!
//SwordDamage Script
using UnityEngine;
using System.Collections;
public class SwordDamage : MonoBehaviour
{
public GameObject target;
public int swordDamage = 5;
public void AttackEnemy(){
EnemyHealth eh = (EnemyHealth)target.GetComponent ("EnemyHealth");
eh.AdjustcurEnemyHealth (-swordDamage);
}
void OnColliderHit(){
AttackEnemy ();
}
void Update(){
if (Input.GetButtonDown ("Fire1")){
// runs this code when the fire button is pressed down
animation.Play(); // this will play the default animation on this object
}
}
}
//Players Health script
using UnityEngine;
using System.Collections;
public class PlayerHealth : MonoBehaviour
{
private int maxHealth = 100;
public int curHealth = 100;
private float healthBarLength;
void Start(){
healthBarLength = Screen.width / 2;
}
void Update(){
AdjustcurHealth (0);
}
void OnGUI(){
GUI.Box (new Rect(10,10, healthBarLength, 20),curHealth + "/" + maxHealth);
}
public void AdjustcurHealth(int adj){
curHealth += adj;
if(curHealth <= 0)
curHealth = 0;
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
if(curHealth <= 0)
//Dead
healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
}
}
//enemy's health script
using UnityEngine;
using System.Collections;
public class EnemyHealth : MonoBehaviour
{
public int enemyHealth = 15;
public int curEnemyHealth = 15;
void Update(){
AdjustcurEnemyHealth (0);
}
public void EnemyDied(int d){
Destroy (gameObject);
}
public void AdjustcurEnemyHealth(int adj){
curEnemyHealth += adj;
if(curEnemyHealth <= 0)
curEnemyHealth = 0;
if(curEnemyHealth > enemyHealth)
curEnemyHealth = enemyHealth;
if(enemyHealth < 1)
enemyHealth = 1;
if (curEnemyHealth <= 0)
EnemyDied (0);
}
}
//Enemies attack script
using UnityEngine;
using System.Collections;
public class EnemyAttack : MonoBehaviour
{
public GameObject target;
public int enemyDamage = 15;
public void Attack(){
PlayerHealth eh = (PlayerHealth)target.GetComponent ("PlayerHealth");
eh.AdjustcurHealth (-enemyDamage);
}
void OnColliderHit(){
Attack ();
}
}