我在游戲中有一個腳本,可以使目標昏迷并將其拋向空中。問題是,yield return new WaitForSeconds(0.01f)
眩暈長度和延伸的眩暈長度根本不隨 Time.DeltaTime 縮放,即使它應該。如果游戲速度足夠快,這會導致攻擊永久擊暈敵人。以下是相關代碼:
void Start()
{
coll = gameObject.GetComponent<Collider2D>();
halfLength = stunLength / 2;
baseColor = gameObject.GetComponent<SpriteRenderer>().color;
StartCoroutine("Gust");
}
public IEnumerator Gust()
{
coll.enabled = false;
float sus = halfLength;
while (halfLength > 0)
{
transform.position = new Vector3(0, halfLength * 0.05f, 0);
halfLength -= 0.03f;
yield return new WaitForSeconds(0.01f);
}
while (halfLength < sus)
{
transform.position = new Vector3(0, halfLength * -0.05f, 0);
halfLength = 0.03f;
yield return new WaitForSeconds(0.01f);
}
coll.enabled = true;
StunEnd();
}
Time.TimeScale 正在腳本之外通過 UI 按鈕進行更改。任何幫助將不勝感激。
uj5u.com熱心網友回復:
假設您的游戲以60
每秒幀數運行。
這意味著兩幀之間的最小時間已經是1 / 60 = 0.016666...
。
所以即使在默認timeScale
的
yield return new WaitForSeconds(0.01f);
無論如何已經比默認幀速率短并且yield
無論如何=>這基本上等于使用
yield return null;
所以現在如果你加快你的游戲時間甚至更短,它仍然會相等
yield return null;
yield
至少一幀!
我希望你現在看到這是怎么回事。
與其嘗試使用一些固定的時間間隔,不如直接使用幀并依賴Time.deltaTime
.
您可以嘗試使用類似的東西,例如
如果您想按持續時間和特定的位置偏移量
// adjust this!
public Vector3 stunOffset = Vector3.up;
private IEnumerator MoveTo(Vector3 to, float duration)
{
yield return MoveFromTo(transform.position, to, duration);
}
private IEnumerator MoveFromTo(Vector3 from, Vector3 to, float duration)
{
for(var timePassed = 0f; timePassed < duration; timePassed = Time.deltaTime)
{
transform.position = Vector3.Lerp(from, to , timePassed / duration);
yield return null;
}
transform.position = to;
}
public IEnumerator Gust()
{
coll.enabled = false;
float sus = halfLength;
var initialPosition = transform.position;
var targetPosition = initialPosition stunOffset;
yield return MoveTo(targetPosition, halfLength);
yield return MoveTo(initialPosition halfLength);
coll.enabled = true;
StunEnd();
}
或者,如果您想以固定的移動速度前進
// Adjust these
public float stunSpeed = 1f;
public Vector3 stunOffset = Vector3.up;
private IEnumerator MoveTo(Vector3 to, float speed)
{
while(tranform.position != to)
{
transform.position = Vector3.MoveTowards(transform.position, to, speed * Time.deltaTime);
yield return null;
}
transform.position = to;
}
public IEnumerator Gust()
{
coll.enabled = false;
float sus = halfLength;
var initialPosition = transform.position;
var targetPosition = (stunOinitialPosition stunOffset;
yield return MoveTo(targetPosition, stunSpeed);
yield return MoveTo(initialPosition stunSpeed);
coll.enabled = true;
StunEnd();
}
uj5u.com熱心網友回復:
也許您應該將禁用控制和人群控制的“影片”分成兩個單獨的協程。
IEnumerator DisableControl()
{
coll.enabled = false;
yield return new WaitForSeconds(stunLength);
coll.enabled = true;
}
IEnumerator Gust()
{
//Perform object manipulation/play animation
}
這樣您就可以避免誤用 waitforseconds 和使物件昏迷。它還允許您潛在地重用眩暈邏輯并使用其他“方式”進行人群控制,而無需撰寫重復的代碼
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/467731.html
上一篇:檢測2個物件之間的碰撞