Skip to main content
 首页 » 编程设计

c#之Damage over time effect 正在破坏我的帧率,unity c#

2024年02月06日11over140

好的,大家好。 我对脚本编写还很陌生,我正在通过制作游戏来学习。 我正在尝试对我的播放器应用随时间推移的损坏效果。虽然我有一些有用的东西,但当计时器启动时,我的帧速率从 60 以上下降到 10 以下。所以我希望有人可以帮助解释为什么会这样,如果我的方法可行并且我可以做更好的事情。这是我的代码。

public class AllDamage : MonoBehaviour  
{ 
//Type of Damage to be applied when attatched to a game object 
public bool isFire; 
public bool isIce; 
public bool isElectric; 
public bool isWind; 
public bool isBlunt; 
public bool isSharp; 
public bool isToxic; 
public bool isPoisen; 
 
public bool isTicking; // is this damage over time 
 
public int ticks = 0; 
public int DotDuration; 
 
// Ammount or initial damage 
public float ammountHealthDmg = 10f; 
public float ammountPhysicalDmg = 10f; 
public float ammountMentalDmg = 10f; 
// ammount of damage over time 
public float tickHealthDmg = 1f; 
public float tickPhysicalDmg = 1f; 
public float tickMentalDmg = 1f; 
 
 
 
void Update() 
{ 
    if(isTicking == true) 
    { 
        applyDoT (); 
        StartCoroutine("CountSeconds"); 
    } 
} 
 
IEnumerator CountSeconds() 
{ 
    int DoTDuration = 0; 
    while(true) 
    { 
        for (float timer = 0; timer < 1; timer += Time.deltaTime) 
        yield return 0; 
        DoTDuration++; 
        Debug.Log(DoTDuration + " seconds have passed since the Coroutine started."); 
 
        if(DoTDuration == 10) 
        { 
            StopCoroutine("CountSeconds"); 
            isTicking = false; 
        } 
    } 
} 
void applyDoT() 
{ 
    // ticks increments 60 times per second, as an example 
    ticks++; 
    // Condition is true once every second 
    if(ticks % 60 == 0) 
    { 
        decrementStats(tickHealthDmg, tickPhysicalDmg, tickMentalDmg); 
    } 
} 
 
// is this the player?... what am i?...apply my inital damage...do i have damage over time 
void OnTriggerEnter(Collider other) 
{ 
    if(other.tag == "Player") 
    { 
        if(isFire) 
        { 
            decrementStats(ammountHealthDmg, ammountPhysicalDmg, ammountMentalDmg); 
            isTicking = true; 
        } 
        if(isIce) 
        { 
            decrementStats(ammountHealthDmg, ammountPhysicalDmg, ammountMentalDmg); 
            isTicking = true; 
        } 
        if(isElectric) 
        { 
            decrementStats(ammountHealthDmg, ammountPhysicalDmg, ammountMentalDmg); 
        } 
        if(isWind) 
        { 
            decrementStats(ammountHealthDmg, ammountPhysicalDmg, ammountMentalDmg); 
        } 
        if(isBlunt) 
        { 
            decrementStats(ammountHealthDmg, ammountPhysicalDmg, ammountMentalDmg); 
        } 
        if(isSharp) 
        { 
            decrementStats(ammountHealthDmg, ammountPhysicalDmg, ammountMentalDmg); 
            isTicking = true; 
        } 
        if(isToxic) 
        { 
            decrementStats(ammountHealthDmg, ammountPhysicalDmg, ammountMentalDmg); 
            isTicking = true; 
        } 
        if(isPoisen) 
        { 
            decrementStats(ammountHealthDmg, ammountPhysicalDmg, ammountMentalDmg); 
            isTicking = true; 
        } 
    } 
} 
// - stat values 
void decrementStats(float health, float physical, float mental) 
{ 
    Stats.health -= health; 
    Stats.physical -= physical; 
    Stats.mental -= mental; 
    Stats.CheckAllStats(); 
} 

请您参考如下方法:

您通过在 Update() 回调中调用 StartCoroutine() 多次启动协程,每帧一次。 你应该做的是,当你想启动协程时,将 shouldStartTicking (在你的代码中称为 isTicking )标志设置为 true 然后在协程中立即将其设置为 false。 但是一个更简洁的解决方案是直接从注册命中的代码启动协程。 我还建议您从协程中调用 decrementStats 方法,而不是使用外部反变量来传达 DoT 效果的状态。