Unity2D:创建波浪分数文本动画

时间:2018-05-05 19:33:27

标签: unity3d

我希望每次玩家拿起一枚硬币以及制作另一个波浪文本动画时,将我的乐谱文本动画为波浪动画,但每次玩家丢失一些硬币时都会采用相反的方式。我已经有一个代码(如下所示)使某个文本做一个波浪动画,但为了做到这一点,我分配了文本的每个字符要么向上或向下以最初制作波浪动画,如下所示:

gif

我的问题是:如果我要为分数文本执行此方法,则无法确定玩家将获得多少硬币,因此我很难估计多少钱我需要分数文字,因为玩家在分数中添加新数字的可能性是随机的。

那么我有没有办法为我的乐谱文本创建波浪文本动画?谢谢!

public GameObject WaveTextObj, text1, text2, text3, text4;

// Use this for initialization
void Start () {
    text1.SetActive (false);
    text2.SetActive (false);
    text3.SetActive (false);
    text4.SetActive (false);
    StartCoroutine (WaveEffect ());
}

IEnumerator WaveEffect() {
    while (true) {
       WaveTextObj.SetActive (true);              
        yield return new WaitForSeconds (0.3f);
        text1.SetActive (true);             
        yield return new WaitForSeconds (0.08f);
        text2.SetActive (true);           
        yield return new WaitForSeconds (0.17f);
        text3.SetActive (true);         
        yield return new WaitForSeconds (0.19f);
        text4.SetActive (true);        
        yield return new WaitForSeconds(0.4f);
        WaveTextObj.SetActive (false);
    }
}

NEW UPDATE

 text = "Testing";
 string[] characters = new string[text.Length];
 for (int i = 0; i < text.Length; i++)
 {
       characters[i] = text[i].ToString();
 }

1 个答案:

答案 0 :(得分:1)

使用Mathf.Sin(Time.time)

public GameObject WaveTextObj;
public GameObject textPrefab;

IEnumerator WaveEffect(string text) {
    //clear out the existing text
    while(WaveTextObj.transform.childCount > 0) {
        Destroy(WaveTextObj.transform.GetChild(0));
    }
    //create new text
    float scalar = 0.1f;
    float timeScalar = 3;
    for (int i = 0; i < text.Length; i++)
    {
       GameObject o = Instantiate(textPrefab, new Vector3(i * scalar,0,0), Quaternion.Identity, WaveTextObj.transform)
       o.GetComponent<Text>().text = text[i].ToString();
    }
    //animate text
    while (true) {
        int i = 0;
        foreach (Transform child in transform) {
            Vector3 p = child.localPosition;
// By setting each text object's y position to a value controlled by a sine wave,
// they will jiggle up and down. Their index in the string handles each letter being
// at a different point on the wave.
            p.y = Mathf.Sin((Time.time + i * scalar) * timeScalar) * scalar;
            child.localPosition = p;
            i++;
        }
        yield return new WaitForEndOfFrame();
    }
}

由您决定如何退出循环。有几个值是任意的。