- Home /
I want to attack me enemy and decrease their health, but t won't work, what am I doing wrong with my script?
For some time, I have been looking up tutorials to write a script in which my player can use a weapon and that weapon will damage the enemy and take away their health. Now, I have a script that the enemy damages the layer and takes away their health tat works, so I took it and tried to reverse the parts but it still does not work. What do I need to make it work?
The Script Below:
var Damage : int = 50;
var Animation;
private var enemy : GameObject;
private var enemyHealth : EnemyHealth;
private var enemyInRange : boolean;
function Awake ()
{
Animation = GetComponent.<Animation>();
enemy = GameObject.FindGameObjectWithTag ("Enemy");
enemyHealth = enemy.GetComponent (EnemyHealth);
}
function Update ()
{
if(Input.GetButtonDown("Fire1"))
{
GetComponent.<Animation>().Play("Pick");
}
}
function OnTriggerEnter (other : Collider)
{
if(other.gameObject == enemy)
{
enemyInRange = true;
Debug.Log("Enemy Hit");
}
}
function OnTriggerExit (other : Collider)
{
if(other.gameObject == enemy)
{
enemyInRange = false;
}
}
function Attack ()
{
if(enemyHealth.Health > 0)
{
enemyHealth.ApplyDamage (Damage);
}
}
and the script for my enemy health:
var Health = 100;
function Update ()
{
if(Health <= 0)
{
Dead();
}
}
function ApplyDamage(TheDamage : int)
{
Debug.Log("Damaged");
Health -= TheDamage;
}
function Dead()
{
Destroy(gameObject);
}
Answer by rimawi · Dec 07, 2016 at 12:07 AM
You need to call Damage function. your attack which is supposed to call damage is never called.
function OnTriggerEnter (other : Collider)
{
if(other.gameObject == enemy)
{
enemyInRange = true;
Debug.Log("Enemy Hit");
Attack();
}
}
---------THE ATTACK FUNCTION--------------
function Attack () {
Debug.Log("BEING ATTACKED");
if(enemyHealth.Health > 0) {
enemyHealth.ApplyDamage (Damage);
}
}
sorry attack should be Attack (); make sure it is initial cap in the code
make sure Attack(); Is with initial cap 2- Add a debug to the Attack function
function Attack () { Debug.Log("BEING ATTAC$$anonymous$$ED"); if(enemyHealth.Health > 0) { enemyHealth.ApplyDamage (Damage); } }
This works, but a comment below says that it'll only work for one enemy ins$$anonymous$$d of multiple, is this true?
Answer by RobAnthem · Dec 08, 2016 at 06:46 AM
Okay I wrote up a quick script, attach the ColliderCombat to the weapon, and the EntityScript to the player and the enemies, and just set the EntityType, I also made it so if you want NPC's seperately, the wont be involved in the combat. Also added an HP handler. I have not tested it though. EDIT: The purpose of the "entityCollider" is incase you have other colliders on your player object, it ensures that only the weapon collider and the entity collider can provide the event. Collider Script is:
using UnityEngine;
using System.Collections;
public class ColliderCombat : MonoBehaviour
{
#region Data
public int minDamage;
public int maxDamage;
#endregion
void OnCollisionEnter(Collision collision)
{
EntityScript defender = collision.collider.gameObject.GetComponent<EntityScript>();
if (gameObject.GetComponent<EntityScript>().entityType == EntityScript.EntityType.Enemy &&
defender.entityType == EntityScript.EntityType.Player)
{
if (collision.collider == defender.entityCollider)
{
defender.HitPoints -= Random.Range(minDamage, maxDamage);
}
}
else if (gameObject.GetComponent<EntityScript>().entityType == EntityScript.EntityType.Player &&
defender.entityType == EntityScript.EntityType.Enemy)
{
if (collision.collider == defender.entityCollider)
{
defender.HitPoints -= Random.Range(minDamage, maxDamage);
}
}
}
Entity Script:
using UnityEngine;
using System.Collections;
public class EntityScript : MonoBehaviour
{
#region Data
public Collider entityCollider;
public int maxHitPoints = 100;
private int hitPoints = 100;
public int hpRegen = 1;
public int regenInterval = 60;
private int regenSwitch = 0;
public int HitPoints { get { return hitPoints; } set
{
hitPoints = value;
if (hitPoints <= 0) { deathEvent(); }
} }
public enum EntityType { Player, Enemy, NPC }
public EntityType entityType;
#endregion
void Update()
{
regenSwitch++;
if (regenSwitch > 60)
{
regenSwitch = 0;
if (HitPoints < maxHitPoints - hpRegen)
{
HitPoints += hpRegen;
}
else
{
HitPoints = maxHitPoints;
}
}
}
private void deathEvent()
{
//Death stuff here
}
}
I would use raycast, but every time itry to use raycast it ends up causing errors or no errors and it doesnt work
Answer by federicosalgado · Dec 09, 2016 at 06:18 AM
Maybe this Will help
Enemyscript enemy;
Void OnTriggerEnter(Collider other){
If(other.CompareTag("enemy")){
enemy = Other.GetComponent<the enemy script>.();
enemy.Damage(int);
}
}
Also collider.other is newly deprecated, collision.collider is the main way to check the "other" collider. Or in your code it would be "other.Collider". Since the collision passes a lot of info inside itself, like the points of impact.
Answer by Tihan-Nico · Dec 09, 2016 at 10:02 PM
@Draggonnoble
Health C#
using UnityEngine; public class Health : MonoBehaviour { public const int maxHealth = 100; %|-1847962930_2|% public void TakeDamage(int amount) %|493011656_3|% currentHealth -= amount; %|2022189198_5|% %|-731059696_6|% %|-183514112_7|% Debug.Log("Dead!"); } %|916447909_10|% }
Bullet C#
using UnityEngine; using System.Collections; public class Bullet : MonoBehaviour { %|420083905_11|% %|1400404717_12|% %|-2020662319_13|% %|464509255_14|% %|-330945156_15|% if (health != null) %|-222940933_17|% health.TakeDamage(10); %|-1844065826_19|% %|59224891_20|% } }
Your answer
Follow this Question
Related Questions
I use this script, but the enemy lose health if i don´t target him. 1 Answer
How to make my player not lose health when attacking the enemy? 2 Answers
Damage script is screwed up...? what to do? 1 Answer
Script Wont Take away health from my player when hit by enemy bullet. 0 Answers
Attack,Health and enemy health. 1 Answer