Синтаксис:
Используется csharp
using UnityEngine;
using System.Collections;
public class EnemyAttack : MonoBehaviour {
public GameObject target;
public float coolDown;
private float attackTimer;
// Use this for initialization
void Start () {
attackTimer = 0;
if (coolDown == 0) {
coolDown = 2.0f;
}
}
// Update is called once per frame
void Update () {
if (attackTimer > 0) {
attackTimer -= Time.deltaTime;
}
if (attackTimer < 0) {
attackTimer = 0;
}
if (attackTimer == 0) {
Attack();
}
}
private void Attack() {
Debug.Log("Player attacking");
float distance = Vector3.Distance(target.transform.position, transform.position);
Vector3 dir = (target.transform.position - transform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
if (distance < 2.2f && direction > 0) {
PlayerHealth ph = (PlayerHealth)target.GetComponent("PlayerHealth");
ph.AddjustCurrentHealth(-10);
attackTimer = coolDown;
}
}
}
using System.Collections;
public class EnemyAttack : MonoBehaviour {
public GameObject target;
public float coolDown;
private float attackTimer;
// Use this for initialization
void Start () {
attackTimer = 0;
if (coolDown == 0) {
coolDown = 2.0f;
}
}
// Update is called once per frame
void Update () {
if (attackTimer > 0) {
attackTimer -= Time.deltaTime;
}
if (attackTimer < 0) {
attackTimer = 0;
}
if (attackTimer == 0) {
Attack();
}
}
private void Attack() {
Debug.Log("Player attacking");
float distance = Vector3.Distance(target.transform.position, transform.position);
Vector3 dir = (target.transform.position - transform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
if (distance < 2.2f && direction > 0) {
PlayerHealth ph = (PlayerHealth)target.GetComponent("PlayerHealth");
ph.AddjustCurrentHealth(-10);
attackTimer = coolDown;
}
}
}