首页 > 解决方案 > 动画后世界位置变化 - Unity

问题描述

我是 Unity 的新手,我正在尝试在我的卡片堆栈中实现滑动手势,设法通过使用预制件使用脚本创建一个堆栈,堆栈是动态的,最多可以有 220 张卡片。

在此处输入图像描述

问题是当我向左滑动时的手势应该从堆栈的前面移除卡片,并且应该将另一张卡片添加到堆栈的后面,总共应该只有4 张卡片可见

Snippet #1我正在创建我的游戏对象并将它们设置为活动/非活动

public void SetCardNumber ()
{
    Positions = new Vector3[4];
    CardScales = new Vector3[4];
    Positions [0] = new Vector3 (Screen.width / 2, (Screen.height / 2) + 135f, 0f);
    Positions [1] = new Vector3 (Screen.width / 2, (Screen.height / 2) + 90f, 0f);
    Positions [2] = new Vector3 (Screen.width / 2, (Screen.height / 2) + 45f, 0f);
    Positions [3] = new Vector3 (Screen.width / 2, Screen.height / 2, 0f);

    CardScales [0] = new Vector3 (0.85f, 0.85f, 0f);
    CardScales [1] = new Vector3 (0.9f, 0.9f, 0f);
    CardScales [2] = new Vector3 (0.95f, 0.95f, 0f);
    CardScales [3] = new Vector3 (1f, 1f, 0f);

    InActivePositions = new Vector3 (0f, 0f, 0f);
    InActiveCardScales = new Vector3 (1f, 1f, 0f);

    Cards = new GameObject[words.Length];
    CardNumber = new GameObject[words.Length];
    CardText = new GameObject[words.Length];
    ShowSentenceBulb = new GameObject[words.Length];
    for (int i = 0, j = words.Length; i <= words.Length - 1; i++,j--) {
        GameObject obj = (GameObject)Instantiate (CardPrefab) as GameObject;
        Cards [i] = obj;
        CurrentCardCounter++;

        Cards [i].transform.SetParent (panel.transform, false);
        foreach (Transform child in obj.transform) {
            if (child.tag == "TextCardNumber")
                CardNumber [i] = child.gameObject;
            if (child.tag == "TextCard")
                CardText [i] = child.gameObject;
            if (child.tag == "ShowSentenceBulb")
                ShowSentenceBulb [i] = child.gameObject;
        }

        if (i == 0) {
            CardText [i].GetComponent<Text> ().text = CurrentWordList [j - 1];
            CardText [i].GetComponent<Text> ().fontSize = 70;
            CardText [i].GetComponent<Text> ().color = new Color (255, 215, 0);
        } else
            CardText [i].GetComponent<Text> ().text = CurrentWordList [j - 1];
        CardNumber [i].GetComponent<Text> ().text = j + "";
        ShowSentenceBulb [i].GetComponent<Button> ().onClick.AddListener (ShowSentenceOnBulbClick);
    }

    var quotient = CurrentCardCounter / 4;

    for (int k = 3; CurrentCardCounter > 0; CurrentCardCounter--,k--) {
        if (CurrentCardCounter <= (4 * (quotient - 1))) {
            Cards [CurrentCardCounter - 1].SetActive (false);
        }
        else {
            Cards [CurrentCardCounter-1].transform.position = Positions [k];
            Cards [CurrentCardCounter-1].transform.localScale = CardScales [k];
            Cards [CurrentCardCounter-1].SetActive (true);
        }

    }
    CurrentCardCounter=words.Length-1;
    swipeAnim= Cards[CurrentCardCounter].GetComponent<Animator>();
}

我处理这个问题的方法是在场景开始时创建所有游戏对象,但只激活 4 个,并且每当我向左滑动时,一张新卡就会设置为 Active。

Snippet #2向左滑动手势以从堆栈中移除前卡

if (swipeLeft && CurrentCardCounter > 0 && !isCardShowingSentence) {

        swipeAnim.enabled = true;
        swipeAnim.Play ("swipe");
        if (CurrentCardCounter > 0) {
            CurrentCardCounter--;
            swipeAnim = Cards [CurrentCardCounter].GetComponent<Animator> ();

            if (CurrentCardCounter > 3) {
                Cards [CurrentCardCounter].transform.position = Positions [3];
                Cards [CurrentCardCounter].transform.localScale = CardScales [3];
                Cards [CurrentCardCounter - 1].transform.position = Positions [2];
                Cards [CurrentCardCounter - 1].transform.localScale = CardScales [2];
                Cards [CurrentCardCounter - 2].transform.position = Positions [1];
                Cards [CurrentCardCounter - 2].transform.localScale = CardScales [1];
                Cards [CurrentCardCounter - 3].SetActive (true);
                Cards [CurrentCardCounter - 3].transform.position = Positions [0];
                Cards [CurrentCardCounter - 3].transform.localScale = CardScales [0];
            } else if (CurrentCardCounter > 2) {
                Cards [CurrentCardCounter].transform.position = Positions [3];
                Cards [CurrentCardCounter].transform.localScale = CardScales [3];
                Cards [CurrentCardCounter - 1].transform.position = Positions [2];
                Cards [CurrentCardCounter - 1].transform.localScale = CardScales [2];
                Cards [CurrentCardCounter - 2].transform.position = Positions [1];
                Cards [CurrentCardCounter - 2].transform.localScale = CardScales [1];
                Cards [CurrentCardCounter - 3].SetActive (true);
                Cards [CurrentCardCounter - 3].transform.position = Positions [0];
                Cards [CurrentCardCounter - 3].transform.localScale = CardScales [0];
            } else if (CurrentCardCounter > 1) {
                Cards [CurrentCardCounter].transform.position = Positions [3];
                Cards [CurrentCardCounter].transform.localScale = CardScales [3];
                Cards [CurrentCardCounter - 1].transform.localScale = CardScales [2];
                Cards [CurrentCardCounter - 1].transform.position = Positions [2];
                Cards [CurrentCardCounter - 2].transform.localScale = CardScales [1];
                Cards [CurrentCardCounter - 2].transform.position = Positions [1];
            } else if (CurrentCardCounter > 0) {
                Cards [CurrentCardCounter].transform.localScale = CardScales [3];
                Cards [CurrentCardCounter].transform.position = Positions [3];
                Cards [CurrentCardCounter - 1].transform.localScale = CardScales [2];
                Cards [CurrentCardCounter - 1].transform.position = Positions [2];
            }
        } else {
            //swipeAnim = Cards [0].GetComponent<Animator> ();
            //swipeAnim.enabled = true;
            //swipeAnim.Play ("shake");
            //swipeAnim.enabled = false;
        }

    }

这一切都得到了优雅的处理,但还有一个向右滑动手势(为了取回刷过的卡片,刷过的卡片被添加到堆栈的前面,并且添加到后面的卡片将再次变为非活动状态)这搞砸了游戏对象的位置,我不知道为什么。

Snippet #3向右滑动手势以取回刷过的卡片并从堆栈中取出背面的卡片

if (swipeRight && CurrentCardCounter < words.Length - 1  && !isCardShowingSentence) {

        if (CurrentCardCounter < words.Length) {
            swipeAnim = Cards [CurrentCardCounter+1].GetComponent<Animator> ();
            swipeAnim.enabled = true;
            swipeAnim.Play ("swipeBack");
            CurrentCardCounter++;
        }

        if (CurrentCardCounter > 6) {
            Cards [CurrentCardCounter - 1].transform.position = Positions[3];
            Cards [CurrentCardCounter - 1].transform.localScale = CardScales[3];
            Cards [CurrentCardCounter - 2].transform.position = Positions[2];
            Cards [CurrentCardCounter - 2].transform.localScale = CardScales[2];
            Cards [CurrentCardCounter - 3].transform.position = Positions[1];
            Cards [CurrentCardCounter - 3].transform.localScale = CardScales[1];
            Cards [CurrentCardCounter - 4].SetActive (false);
            Cards [CurrentCardCounter - 4].transform.position = InActivePositions;
            Cards [CurrentCardCounter - 4].transform.localScale = InActiveCardScales;
        }else if (CurrentCardCounter > 5) {
            Cards [CurrentCardCounter - 1].transform.position = Positions[3];
            Cards [CurrentCardCounter - 1].transform.localScale = CardScales[3];
            Cards [CurrentCardCounter - 2].transform.position = Positions[2];
            Cards [CurrentCardCounter - 2].transform.localScale = CardScales[2];
            Cards [CurrentCardCounter - 3].transform.position = Positions[1];
            Cards [CurrentCardCounter - 3].transform.localScale = CardScales[1];
            Cards [CurrentCardCounter - 4].SetActive (false);
            Cards [CurrentCardCounter - 4].transform.position = InActivePositions;
            Cards [CurrentCardCounter - 4].transform.localScale = InActiveCardScales;
        }else if (CurrentCardCounter > 4) {
            Cards [CurrentCardCounter - 1].transform.position = Positions[3];
            Cards [CurrentCardCounter - 1].transform.localScale = CardScales[3];
            Cards [CurrentCardCounter - 2].transform.position = Positions[2];
            Cards [CurrentCardCounter - 2].transform.localScale = CardScales[2];
            Cards [CurrentCardCounter - 3].transform.position = Positions[1];
            Cards [CurrentCardCounter - 3].transform.localScale = CardScales[1];
            Cards [CurrentCardCounter - 4].SetActive (false);
            Cards [CurrentCardCounter - 4].transform.position = InActivePositions;
            Cards [CurrentCardCounter - 4].transform.localScale = InActiveCardScales;
        } else if (CurrentCardCounter > 3) {
            Cards [CurrentCardCounter - 1].transform.position = Positions[3];
            Cards [CurrentCardCounter - 1].transform.localScale = CardScales[3];
            Cards [CurrentCardCounter - 2].transform.position = Positions[2];
            Cards [CurrentCardCounter - 2].transform.localScale = CardScales[2];
            Cards [CurrentCardCounter - 3].transform.position = Positions[1];
            Cards [CurrentCardCounter - 3].transform.localScale = CardScales[1];
            Cards [CurrentCardCounter - 4].SetActive (false);
            Cards [CurrentCardCounter - 4].transform.position = InActivePositions;
            Cards [CurrentCardCounter - 4].transform.localScale = InActiveCardScales;
        } else if (CurrentCardCounter > 2) {
            Cards [CurrentCardCounter - 1].transform.position = Positions[3];
            Cards [CurrentCardCounter - 1].transform.localScale = CardScales[3];
            Cards [CurrentCardCounter - 2].transform.position = Positions[2];
            Cards [CurrentCardCounter - 2].transform.localScale = CardScales[2];
            Cards [CurrentCardCounter - 3].transform.position = Positions[1];
            Cards [CurrentCardCounter - 3].transform.localScale = CardScales[1];
        } else if (CurrentCardCounter > 1) {
            Cards [CurrentCardCounter - 1].transform.position = Positions[3];
            Cards [CurrentCardCounter - 1].transform.localScale = CardScales[3];
            Cards [CurrentCardCounter - 2].transform.position = Positions[2];
            Cards [CurrentCardCounter - 2].transform.localScale = CardScales[2];
        } else if (CurrentCardCounter > 0) {
            Cards [CurrentCardCounter - 1].transform.position = Positions[3];
            Cards [CurrentCardCounter - 1].transform.localScale = CardScales[3];
        }
    }

这个问题一直让我头疼,我不知道多久,任何帮助将不胜感激

标签: c#unity3d

解决方案


推荐阅读