2017-10-05 53 views
0

我做了一個統一的測試遊戲,當它點擊一個按鈕時,它會產生一個由工廠類創建的柱面。我試圖在創造圓柱體時做到這一點,其高度在接下來的20秒內縮小。我發現的一些方法很難轉化爲我正在做的事情。如果你能引導我走向正確的方向,我會非常感激。隨着時間的推移縮放遊戲對象

這是我對氣缸類

public class Cylinder : Shape 
{ 
    public Cylinder() 
    { 
    GameObject cylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder); 
     cylinder.transform.position = new Vector3(3, 0, 0); 
     cylinder.transform.localScale = new Vector3(1.0f, Random.Range(1, 2)-1*Time.deltaTime, 1.0f); 

     cylinder.GetComponent<MeshRenderer>().material.color = Random.ColorHSV(); 
     Destroy(cylinder, 30.0f); 
    } 
} 

回答

0

退房Lerp代碼。如何使用它的一般例子是這樣的:

float t = 0; 
Update() 
{ 
    t += Time.deltaTime; 
    cylinder.localScale = new Vector3(1, Mathf.Lerp(2f, 1f, t/3f), 1); // shrink from 2 to 1 over 3 seconds; 
} 
3

這可以用一個協程功能Time.deltaTimeVector3.Lerp來完成。類似於Rotate GameObject over timeMove GameObject over time的問題。修改它有點做到這一點。

bool isScaling = false; 

IEnumerator scaleOverTime(Transform objectToScale, Vector3 toScale, float duration) 
{ 
    //Make sure there is only one instance of this function running 
    if (isScaling) 
    { 
     yield break; ///exit if this is still running 
    } 
    isScaling = true; 

    float counter = 0; 

    //Get the current scale of the object to be moved 
    Vector3 startScaleSize = objectToScale.localScale; 

    while (counter < duration) 
    { 
     counter += Time.deltaTime; 
     objectToScale.localScale = Vector3.Lerp(startScaleSize, toScale, counter/duration); 
     yield return null; 
    } 

    isScaling = false; 
} 

用法

將在20秒內擴展遊戲對象:

StartCoroutine(scaleOverTime(cylinder.transform, new Vector3(0, 0, 90), 20f)); 
+1

'的Vector3 startScaleSize = objectToScale.position;' 應該 '的Vector3 startScaleSize = objectToScale.localScale;' – TheSkimek

+0

@TheSkimek是啊,這是正確的。這是一個快速端口。感謝您的更正。 – Programmer

0

您將創建一個新的monobehaviour腳本,並將其添加到您的原始。那麼隨着時間的推移,你將使用monobehaviour(或使用協程)的「更新」方法來更改對象。

Monobehaviour必須是這個樣子:

public class ShrinkBehaviour : MonoBehaviour 
{ 
    bool isNeedToShrink; 
    Config currentConfig; 

    float startTime; 
    float totalDistance; 

    public void StartShrink(Config config) 
    { 
     startTime = Time.time; 
     currentConfig = config; 
     totalDistance = Vector3.Distance(currentConfig.startSize, currentConfig.destinationSize); 
     isNeedToShrink = true; 
     transform.localScale = config.startSize; 
    } 

    private void Update() 
    { 
     if (isNeedToShrink) 
     { 
      var nextSize = GetNextSize(currentConfig); 

      if (Vector3.Distance(nextSize, currentConfig.destinationSize) <= 0.05f) 
      { 
       isNeedToShrink = false; 
       return; 
      } 

      transform.localScale = nextSize; 
     } 
    } 

    Vector3 GetNextSize(Config config) 
    { 
     float timeCovered = (Time.time - startTime)/config.duration; 
     var result = Vector3.Lerp(config.startSize, config.destinationSize, timeCovered); 
     return result; 
    } 

    public struct Config 
    { 
     public float duration; 
     public Vector3 startSize; 
     public Vector3 destinationSize; 
    } 
} 

對於使用這個,你必須做下一個:

public Cylinder() 
{ 
    GameObject cylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder); 
    var shrink = cylinder.AddComponent<ShrinkBehaviour>(); 
    shrink.StartShrink(new ShrinkBehaviour.Config() { startSize = Vector3.one * 10, destinationSize = Vector3.one * 1, duration = 20f }); 
    cylinder.transform.position = new Vector3(3, 0, 0); 

    cylinder.GetComponent<MeshRenderer>().material.color = Random.ColorHSV(); 
    Destroy(cylinder, 30.0f); 
} 

你必須記住,monobehaviour腳本必須是獨立的文件,而且必須有名稱與monobehaviour-class名稱相似。例如,ShrinkBehaviour.cs;

+0

如果我只想縮小物體高度20秒,該怎麼辦? –

+0

哦,對不起,我沒看完。所以我改變我的答案。 –

+0

你沒有聲明速度的定義,你從哪裏獲得速度? –

相關問題