2016-07-25 238 views
0

在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); 
} 

}

+0

可能的[Unity中的簡單事件系統]的副本(http://stackoverflow.com/questions/36244660/simple-event-system-in-unity) – Fattie

+0

您好C-Noobster。你想要了解的是** UnityEvent **。請務必閱讀鏈接的問題。這很容易。 – Fattie

+0

一定要在做鏈接的問題教程,享受 – Fattie

回答

0

您可以創建getter/setter方法來設置/獲取從A類 屬性在你的B類,你可以創建類A的實例並訪問該屬性。

實施例A類:

public class Genre 
{ 
    public string Name { get; set; } 
} 

實施例例如在B類:

Genre genre = new Genre(); 
string name = genre.getName(); 

希望它能幫助。

+0

這是** Unity3D **問題 - 這與Angel無關。這裏的問題涉及Unity中的GameObject結構 – Fattie

+0

Joe Blow,您已經修改了問題並添加了Unity3D標籤。如果您知道原始問題只有C#標記,我不知道您爲什麼投票給我。好難過。 –

+0

嗨@angelGuevara!不,不,我沒有添加Unity3D標籤! ***我只是將它添加到問題的標題中,以免別人浪費時間。***請注意,關於Unity3D的問題絕對是**,沒有別的。 – Fattie

相關問題