- Home /
Player Attacking Enemy Issue
Im having a couple problems with this script. The first problem im having is that var "target" isnt setting as the hit.collider.tag "enemy". And I have multiple enemies in my scene so just dragging one of them from the scene to the inspector wont work. The other problem i need to solve is that when damage is dealt to one enemy in the scene i want the damage only to apply to that enemy. I need a way to call the Enemy_Health script that is attached to that specific target. My enemies are all tagged "Enemy", im not sure if its the problem but they have Character Controllers rather than specific colliders.
var target : GameObject; //The target you want to attack
function FixedUpdate (){
var hit : RaycastHit;
//var ray : Ray = gameObject.camera.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(transform.position, Vector3.forward, hit, 10)){
if(hit.collider.tag == "Enemy"){
target = hit.collider.gameObject;
var enemy = target.transform;
print ("I can hit you");
var dist = Vector3.Distance(enemy.position, transform.position);
if ( dist <= 3) {
if(Input.GetButtonDown("Jump")){
print ("hitting");
target.Enemy_Health.enemyHealth -= 10;
}
}
}
}
}
Your answer
Follow this Question
Related Questions
Attack,Health and enemy health. 1 Answer
Weapon and enemy health 0 Answers
How to get the enemy my player touches when he attack? 1 Answer
Is Triggered Collider Issue 1 Answer