Chcę wysłać wiadomość o odjęciu hp
Skrypt na enemii
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAiNew : MonoBehaviour
{
public float walkSpeed = 5.0f; // poruszanie sie
public float attackDistance = 2.0f; // odleglosc z jaka mozemy zaatakowac
public float attackDemage = 10.0f;
public float attackDelay = 1.0f;//opóźnienei ataku
public float timer = 0;
void OnTriggerStay(Collider other) // ten void wykonuje sie tyle razy jak collider wykrywa kolizje || Equals to jest "=" || jesli inny collider to Player wtedy
{
if (other.tag.Equals("Player"))
{
//quaternion to znaczy od i do || zaznaczenie obiektu, obiekt quaternion || zaznaczenie obiektu target rotation || Look rotation jest to patrzenie w daną stronę
Quaternion targetRotation = Quaternion.LookRotation(other.transform.position - transform.position);
float oryginalX = transform.rotation.x; //te dwie zmienne zapamietuja rotacje jaka jest przed obroceniem
float oryginalZ = transform.rotation.z;//
//quaternionr slerp pozwala nam powoli zmienic parametry obrotu
//parametr rotacji, rotacja do przeciwnika
Quaternion finalRotation = Quaternion.Slerp(transform.rotation, targetRotation, 5.0f* Time.deltaTime); // trzeba pomnozyc przez 5 sekund * czas ponieważ samo 5.0f nic nie znaczy
finalRotation.x = oryginalX; // tutaj jest ta sama rotacja co poprzednio dlatego obiekt sie nie obraca ??
finalRotation.z = oryginalZ;
transform.rotation = finalRotation;
//vector 3 - zwraca odleglosc miedzy a i b
// pozycja naszego o, pozycja gracza - jako other
float distance = Vector3.Distance(transform.position, other.transform.position);// miedzy obiektem a i b
if (distance > attackDistance)
{
transform.Translate(Vector3.forward * walkSpeed * Time.deltaTime); //TRANSLATE - przesuwa pozycje w kierunku wskazanym czyli
}
else // 3 kratki * szybkośc chodzenia * czas
{
if (timer <= 0) { // jesli timer mniejszy od 0 wtedy
other.SendMessage("takeHit", attackDemage); // wyslij metode
timer = attackDelay;
}
}
if (timer > 0) { // jesli timer jest wiekszy od zera wtedy od czasu rzeczywistego odejmij 1 sekunde
timer -= Time.deltaTime;
}
}
}
}
Skrpyt na Pasek mojego zdrowia
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerStats : MonoBehaviour
{
private float maxHealth=100;
private float currentHealth=100;
private float maxArmour=100;
private float currentArmour=100;
public Texture2D healthTexture;
public Texture2D armourTexture;
private float barHeigth;
private float barWidth;
private float canHeal = 0.0f;
private float canRegenerate = 0.0f;
void Awake()
{
barHeigth = Screen.width * 0.01f;
barWidth = Screen.width * 0.15f;
}
void OnGUI()
{
GUI.DrawTexture(new Rect(Screen.width - barWidth - 10,
Screen.height - barHeigth - 10,
currentHealth * barWidth / maxHealth,
barHeigth), healthTexture);
GUI.DrawTexture(new Rect(Screen.width - barWidth - 10,
Screen.height - barHeigth * 2 - 20,
currentArmour * barWidth / maxArmour,
barHeigth),
armourTexture);
}
void takeHit(float demage)
{
if (currentHealth < maxHealth) {
canHeal = 5.0f;
}
if (currentArmour > 0)
{
currentArmour -= demage;
if (currentArmour < 0)
{
currentHealth += currentArmour;
currentArmour = 0;
}} else
{
currentHealth -= demage;
}
currentArmour = Mathf.Clamp(currentArmour, 0, maxArmour);
currentHealth = Mathf.Clamp(currentHealth, 0, maxHealth);
}
void Update()
{
if (canHeal > 0.0f)
{
canHeal -= Time.deltaTime;
}
if (canRegenerate > 0.0f)
{
canRegenerate -= Time.deltaTime;
}
if (Input.GetKeyDown(KeyCode.P)) {
takeHit(30);
}
if (canHeal <= 0.0f && currentHealth < maxHealth)
{
regenerate(ref currentHealth, maxHealth);
}
}
void regenerate(ref float currentStat, float maxStat)
{
currentStat += maxStat * 0.005f;
Mathf.Clamp(currentStat, 0, maxStat);
}
}