- Home /
Attacking enemys
Hi I'm looking for a basic player attack script that works like oblivion's combat system I've tried burgzergarcade but I'm not trying to do the targeting thing I want to be able to walk up to a random enemy and attack them and they loose health.
heres my PlayerAttack script:
var target : GameObject; var attackTimer : float; var coolDown : float; var damage : float = 10;
// Use this for initialization function Start () { attackTimer = 0; coolDown = 1.0f;
}
// Update is called once per frame function Update () { if(attackTimer > 0) attackTimer -= Time.deltaTime;
if (attackTimer < 0)
attackTimer = 0;
if(Input.GetKeyUp(KeyCode.F)) {
if(attackTimer == 0){
Attack();
attackTimer = coolDown;
}
}
} function Attack() { var distance = Vector3.Distance(target.transform.position, transform.position);
var direction = Vector3.Dot((target.transform.position - transform.position).normalized, transform.forward);
Debug.Log(direction);
if(distance < 3) {
if(direction > 0) {
target.GetComponent(EnemyHealth).AddjustCurrentHealth(-damage);
}
}
}
how can I get target to be the enemy infront of me and change when I go to another enemy?
and heres my EnemyHealth script:
var curHealth : float = 20; var maxHealth : float = 20;
function Update () { AddjustCurrentHealth(0);
}
function AddjustCurrentHealth(adj) { curHealth += adj;
if(curHealth < 0)
curHealth = 0;
if (curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
if(curHealth < 1) { //die
Destroy(gameObject);
}
}
Thanks
Answer by TheDemiurge · Apr 24, 2011 at 11:53 AM
Haven't played Oblivion so I can't tell you how to do that, specifically. But I can give you a nice easy way to do damage in general :)
A fairly simple way to do it without going overboard is the SendMessage and BroadcastMessage functions. What they do is tell another GameObject to try to run the function you name, on every MonoBehaviour that's attached to it. The only difference with Send and Broadcast is that Broadcast also takes the "message" to all the children of the object.
So for example you decide in advance that every character has a TakeDamage function that takes 1 parameter - the damage number as a float. So, if your target is a GameObject, you'd say something like target.SendMessage("TakeDamage", 5.0);
with 5.0 being how much damage to apply. Look up the documentation for these functions because there's an optional third parameter that can be handy if you're not 100% positive your target even has the TakeDamage function.
But it doesn't have to be a float, it can be anything:
enum DamageType { Piercing, Blunt, Slashing, Magic }
struct Damage { var damage: float; var type: DamageType; var armorPenetration: float; }
. . . var dmg: Damage; target.SendMessage("TakeDamage", dmg);
Your TakeDamage function just needs to accept a parameter of type Damage in this case, and it's up to do what you need based on that information.
I'm looking for something that kinda targets the enemy's in front of me automatically but thanks
Your answer
Follow this Question
Related Questions
How to make my player not lose health when attacking the enemy? 2 Answers
auto select enemys 3 Answers
About trigger 3 Answers
How to stop enemy within certain distance of player? 2 Answers
Problems with enemy attack script! 0 Answers