1
나는 화합의 대상이 아니므로, 내 플레이어 앞에있는 모든 객체에 태그가 달린 레이캐스팅을 만들고, 그 안에 있다면 영역 내가 F 키를 누르면 각각의 건강이 필요합니다. 누군가 나를 도울 수 있습니까?Physics.RaycastAll 태그가있는 적을 찾기
using UnityEngine;
using System.Collections;
public class meleeAttack : MonoBehaviour {
public GameObject target;
public float attackTimer;
public float coolDown;
private RaycastHit hit;
// Use this for initialization
void Start() {
attackTimer = 0;
coolDown = 0.5f;
}
// 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);
Vector3 dir = (target.transform.position - transform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
Debug.Log(direction);
if (distance < 3 && direction > 0.5) {
enemyhealth eh = (enemyhealth)target.GetComponent("enemyhealth");
eh.AddjustCurrentHealth(-10);
}
}
}
using UnityEngine;
using System.Collections;
public class enemyhealth : MonoBehaviour {
public int maxHealth = 100;
public int currentHealth = 100;
public float healthBarLength;
// Use this for initialization
void Start() {
healthBarLength = Screen.width/2;
}
// Update is called once per frame
void Update() {
AddjustCurrentHealth(0);
}
void OnGUI() {
GUI.Box(new Rect(10, 40, healthBarLength, 20), currentHealth + "/" + maxHealth);
}
public void AddjustCurrentHealth(int adj){
currentHealth += adj;
if (currentHealth < 0)
currentHealth = 0;
if (currentHealth > maxHealth)
currentHealth = maxHealth;
if (maxHealth < 1)
maxHealth = 1;
healthBarLength = (Screen.width/2) * (currentHealth/(float)maxHealth);
}
}