In Unity3D,unity3d, Rückgabewert von einer Klasse/Methode in einer anderen Klasse/Methode in Unity3D
Ich habe eine Waffe Skript und ein Ausdauer-Skript. Was ich jetzt tun will, ist Ausdauer zu verlieren, wenn meine Waffe schwingt.
Ich habe meinen Code ausprobiert und ich habe ein paar Stunden damit herumgespielt.
Ich benutze Einheit, so gibt es mir auch einen Kommentar über die Verwendung von neuen und das ich Addcomponent usw. verwenden sollte, so würde ich eine Antwort auf diese Frage ebenso schätzen!
Hoffentlich ist dieser Beitrag ein bisschen besser in Bezug auf den Titel und Informationen/Layout, wie ich sehr müde bin und wenig Energie. Ich werde eine kurze Essenspause machen, bevor ich zurückkomme!
Hier ist die Ausdauer System:
`
public class HandS : MonoBehaviour {
public int startingHealth = 100;
public int currentHealth;
public int healthReg;
Sword mySword;
bool isRegenHealth;
public float startingStam = 100;
public float currentStam;
public float stamReg;
bool isRegenStam;
void Awake()
{
currentStam = startingStam;
}
void Update()
{
if (currentStam != startingStam && !isRegenStam)
{
StartCoroutine(RegainStamOverTime());
}
}
private IEnumerator RegainStamOverTime()
{
isRegenStam = true;
while (currentStam < startingStam)
{
Stamregen();
ReduceStamina(mySword.stamDrain);
yield return new WaitForSeconds(1);
}
isRegenStam = false;
}
public void Stamregen()
{
currentStam += stamReg;
}
public void ReduceStamina(float _stamDrain)
{
currentStam -= _stamDrain;
}
}
`
Hier ist das Schwert Skript:
using UnityEngine;
using System.Collections;
public class Sword : MonoBehaviour {
static Animator anim;
public GameObject hitbox;
HandS hp = new HandS();
public int Sworddamage = 20;
public float sec = 0.5f;
public float maxStamina = 20;
public float AttackCD;
public float delayBetweenAttacks = 1.5f;
public float stamDrain = 50;
public AudioSource WeaponSource;
public AudioClip WeaponSound;
void Start() {
anim = GetComponentInParent<Animator>();
WeaponSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update() {
attack();
block();
}
public void attack()
{
if (Input.GetButtonDown("Fire1") && Time.time > AttackCD)
{
AttackCD = Time.time + delayBetweenAttacks;
anim.SetBool("IsAttacking", true);
hitbox.SetActive(true);
StartCoroutine(LateCall());
WeaponSource.PlayOneShot(WeaponSound);
Debug.Log("hit");
hp.ReduceStamina(stamDrain);
}
else
{
anim.SetBool("IsAttacking", false);
}
}
public void block()
{
if (Input.GetButtonDown("Fire2"))
{
anim.SetBool("IsBlocking", true);
}
else
{
anim.SetBool("IsBlocking", false);
}
}
IEnumerator LateCall()
{
yield return new WaitForSeconds(sec);
hitbox.SetActive(false);
}
}
Mögliches Duplikat von [Einfaches Ereignissystem in Unity] (http://stackoverflow.com/questions/36244660/simple-event-system-in-unity) – Fattie
Hallo C-Noobster. Was du als nächstes erfahren möchtest, ist ** UnityEvent **. Lesen Sie sich die verlinkte Frage durch. Es ist sehr leicht. – Fattie
Achten Sie darauf, das Tutorial auf der verknüpften Frage zu tun, genießen Sie – Fattie