在Unity3D中,我有一個武器腳本和一個耐力腳本。我現在想做的是在我的武器搖擺時耗盡體力。UNITY3D,Unity3D中從一個類/方法返回另一個類/方法的值
我想我的代碼,我一直在玩與它周圍的幾個小時。
我用的團結,這也給了我關於使用新的,我應該用Add.component等評論,所以我將不勝感激這個問題的答案的!
希望這個帖子的標題和信息/佈局方面好一點,因爲我很累,低能量。在我回來之前,我要休息一下。
這裏是耐力系統:
`
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;
}
}
`
這裏是劍腳本:
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);
}
}
可能的[Unity中的簡單事件系統]的副本(http://stackoverflow.com/questions/36244660/simple-event-system-in-unity) – Fattie
您好C-Noobster。你想要了解的是** UnityEvent **。請務必閱讀鏈接的問題。這很容易。 – Fattie
一定要在做鏈接的問題教程,享受 – Fattie