Скрипт на здоровье :
Синтаксис:
Используется csharp
using UnityEngine;
using System.Collections;
public class EnemyHealth : MonoBehaviour {
public int maxHealth = 100;
public int curHealth = 100;
public float healthBarLength;
void Start () {
healthBarLength = Screen.width/2;
if(maxHealth<1) maxHealth=1;
curHealth = maxHealth;
}
void Update () {
ShowCurrentHealth(curHealth);
}
void OnGUI () {
GUI.Box(new Rect(10,40,healthBarLength,20),curHealth+"/"+maxHealth);
}
public void ShowCurrentHealth(int sch){
curHealth = sch;
if(curHealth<0) curHealth = 0;
if(curHealth>maxHealth) curHealth = maxHealth;
healthBarLength = (Screen.width/2) * (curHealth / (float)maxHealth);
}
}
using System.Collections;
public class EnemyHealth : MonoBehaviour {
public int maxHealth = 100;
public int curHealth = 100;
public float healthBarLength;
void Start () {
healthBarLength = Screen.width/2;
if(maxHealth<1) maxHealth=1;
curHealth = maxHealth;
}
void Update () {
ShowCurrentHealth(curHealth);
}
void OnGUI () {
GUI.Box(new Rect(10,40,healthBarLength,20),curHealth+"/"+maxHealth);
}
public void ShowCurrentHealth(int sch){
curHealth = sch;
if(curHealth<0) curHealth = 0;
if(curHealth>maxHealth) curHealth = maxHealth;
healthBarLength = (Screen.width/2) * (curHealth / (float)maxHealth);
}
}
Скрипт на удар:
Синтаксис:
Используется csharp
using UnityEngine;
using System.Collections;
public class PlayerAttack : MonoBehaviour {
public GameObject target;
void Start () {
GameObject nearEnemy = GameObject.FindGameObjectWithTag("Enemy");
target = nearEnemy;
}
void Update () {
if(Input.GetKeyDown(KeyCode.F)) {
Attack ();
}
}
private void Attack () {
Debug.Log("Player Attacking!");
EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
eh.ShowCurrentHealth(-10);
}
}
using System.Collections;
public class PlayerAttack : MonoBehaviour {
public GameObject target;
void Start () {
GameObject nearEnemy = GameObject.FindGameObjectWithTag("Enemy");
target = nearEnemy;
}
void Update () {
if(Input.GetKeyDown(KeyCode.F)) {
Attack ();
}
}
private void Attack () {
Debug.Log("Player Attacking!");
EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
eh.ShowCurrentHealth(-10);
}
}
А еще как сделать чтобы меню со здоровьем было допустим не вверху экрана а снизу посередине, просто если делать попиксельное смещение то на всех разрешениях экрана будет по-разному смотреться, а если делать через width/2 то больше половины экрана зайти не могу, пытался подставить % или после слэша дробь типа 5/2, не сработало. Как еще можно двигать панельки по экрану чтобы не было зависимости от разрешения экрана(просто если делать допустим смещение на 500 пикселей влево то я на экране предпросмотра уже не вижу нужную панель, а на разных разрешениях экрана она будет скакать со смещением).
Заранее спасибо.