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
Unless it has changed since I last checked, the answer below still applies: There is no way to capture an OnPointerUp event unless the user first initiated an OnPointerDown event on the same object. You can get around this in a number of ways. I have found that I strongly prefer coding my own input logic, rather than relying on Unity's events. You can piggyback off their events to apply your own logic quite easily. I've elaborated on this in my answer below. Hope it helps!
– AlwaysSunny