- Home /
Edit this script so that the player stops at a distance and attack?
Hello! Is there a way for this script to be edited so that the player stops at a certain distance before the enemy and then start attacking? Or maybe it's got to do with the ClickToMove script that i use? (http://wiki.unity3d.com/index.php/Click_To_Move_C the one i use). Any tips on how to approach such a task or even the code itself would be awesome. Thanks!
using UnityEngine; using System.Collections;
public class PlayerAttack : MonoBehaviour { public GameObject target; public float attackTimer; public float coolDown;
// Use this for initialization
void Start () {
attackTimer = 0;
coolDown = 2.0f;
target = GameObject.FindWithTag("Enemy");
}
// Update is called once per frame
void Update () {
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer = 0;
if(Input.GetKeyUp(KeyCode.F)){
if(attackTimer == 0)
Attack();
attackTimer = coolDown;
}
}
private void Attack(){
float distance = Vector3.Distance(target.transform.position, transform.position);
Debug.Log (distance);
if(distance < 2.5f) {
EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
eh.AdjustCurrentHealth(-10);
}
}
}
Comment