2 回答

TA貢獻2021條經驗 獲得超8個贊
您創建一個 BulletManager.cs(將其附加到始終處于活動狀態的對象或空游戲對象)
public static BulletManager instance;
private void Awake
{
if ( instance == null ) //this creates a Singleton so you can access it directly from everywhere, won't go deep into explaining how it works exactly
{
instance = this;
}
else
{
Destroy (gameObject);
}
}
public int machinegunDmg; //set the initial values in the editor
public int shotgunDmg;
public int skeletonDmg;
現在用適當的標簽標記所有預制件,假設您為機槍射彈預制件使用“MachineGunProj”標簽。
您附加到所有預制件的相同腳本應該會受到該 BulletManager 腳本的損壞,具體取決于您實例化的預制件。
private int DamageOnHit;
//this will get called everytime you instantiate a new prefab that holds this script; it will check for its own tag and depending on it will set the damage in this script to be equal to the appropriate value from BulletManager.cs
private void Start
{
if(this.gameObject.CompareTag("MachineGunProj"))
{
this.DamageOnHit = BulletManager.instance.machinegunDmg;
}
else if(this.gameObject.CompareTag("ShotgunProj"))
{
this.DamageOnHit = BulletManager.instance.shotgunDmg;
}
//else if -- do the same for every prefab you have
}
至于升級,您需要更改 BulletManager.cs 中的值。例如:
public void UpgradeMachineGun()
{
BulletManager.instance.machinegunDmg++; //next time you spawn a machinegun prefab it will take the upgraded value
}
*我直接在這里編寫了上面的代碼,沒有任何文本編輯器或其他任何東西的幫助,所以我可能錯過了一些東西,但總的來說,這是它應該如何工作的想法。如果有什么不起作用,我將非常樂意為您提供進一步的幫助:)

TA貢獻1797條經驗 獲得超6個贊
兩種選擇:
在射彈的
DamageOnHit
每個實例上設置每次
Instantiate
創建新的射彈預制件時,獲取其Projectile
組件并將其設置DamageOnHit
為所需的值。
。每次游戲重新啟動時,復制每個預制資源
我們將它們稱為“ProjectileShotgunProto”和“ProjectileSkeletonProto”。
Instantiate(ProjectileShotgunProto)
當玩家射擊時您將調用它們,而不是實例化您的原始預制件。
無論如何,請勿更改代碼中的原始預制資源,否則會導致問題。
- 2 回答
- 0 關注
- 149 瀏覽
添加回答
舉報