Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to animate dealing of a card deck

Discussion in 'Scripting' started by pKallv, Oct 11, 2014.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    I have a card deck, for test only 30 cards, that i am trying to animate during dealing. Move each card from one position to another. I have some problem with this during my testing.

    Problem is that it does not deal all cards. I have been searching for a solution that i understand but not been able to find anything useful. I am thinking of trying to use like a coroutine or something but i cannot get this work.

    The effect of this code is that only one card is moved.

    The code i have for testing is this:

    Code (csharp):
    1.  
    2. public GameObject selected_GameObject2;
    3. public Vector3 pointA;
    4. public Vector3 pointB;
    5. public float time = 2.0f;
    6. public float i = 0.0f;
    7. public float rate = 0.0f;
    8. private int yy = 0;
    9.  
    10. public List<GameObject> xList = newList<GameObject> ();
    11.  
    12.  
    13.  
    14. void Start() {
    15.  
    16.    pointA = newVector3 (-15f, -15f, 0f); //Vector3.zero;
    17.    pointB = newVector3 (3f, 3f, 0f);
    18.  
    19.    selected_GameObject2.transform.position = pointA;
    20.  
    21.    for (int i = 0; i < 30; i++) {
    22.       xList.Add(selected_GameObject2);
    23.  
    24.       Instantiate(selected_GameObject2);
    25.     }
    26.  }
    27.  
    28.  
    29.  
    30.  
    31. void Update() {
    32.    //MoveObject (selected_GameObject2.transform, pointA, pointB, time);
    33.  
    34.    if (yy < 30) {
    35.       MoveObject (xList[yy].transform, pointA, pointB, time);
    36.       print (yy);
    37.       yy++;
    38.     }
    39.  }
    40.  
    41.  
    42. void MoveObject (Transform thisTransform, Vector3 startPos, Vector3 endPos, float time) {
    43.  
    44.    rate = 1.0f / time;
    45.  
    46.    if (i < 1.0) {
    47.       i += Time.deltaTime * rate;
    48.       thisTransform.position = Vector3.Lerp(startPos, endPos, i);
    49.     }
    50.  }
    51.  
    As i do not understand fully how to implement this i would like to ask for a few tip's, help or a pointer were i can find more information.
     
  2. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,353
    Download iTween plugin. It's free and open source (therefore can be non-commercial). iTween is basically set of useful functions which allow you to transform, rotate, scale your gameobject and much more.

    iTween Official Website
    iTween Documentation

    After importing iTween into your Unity project, use this:

    Code (CSharp):
    1. iTween.MoveTo(this.gameObject, Vector3(100, 100, 100), 0.5f);
    This moves your gameobject (which has this code attached) to x = 100, y = 100 and z = 100 smoothly in 0.5 seconds.

    Hope this helps :)
     
  3. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    Thanks for your response :) ...as their webpage is unavailable i could not find any details so i continue trying to solve this myself and finally found a solution.

    This may not be the most elegant solution but it works. Well, it is a test-script so the loading of GamObjects is just for testing purposes.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class containerScript : MonoBehaviour {
    7.  
    8.    public GameObject theGameObject;
    9.    public GameObject theGameObject1;
    10.    public GameObject theGameObject2;
    11.    public GameObject theGameObject3;
    12.    public GameObject theGameObject4;
    13.    public GameObject theGameObject5;
    14.    public GameObject theGameObject6;
    15.    public GameObject theGameObject7;
    16.    public GameObject theGameObject8;
    17.    public GameObject theGameObject9;
    18.    public GameObject theGameObject10;
    19.    public Vector3 startPos;
    20.    public Vector3 endPos;
    21.    public float rate = 0.5f;
    22.    public float secRate = 1f;
    23.    private List<GameObject> xList = newList<GameObject> ();
    24.    public AudioClip deal;
    25.  
    26.    void Start () {
    27.       print ("Start");
    28.       startPos = newVector3 (-4.0f, -4.0f, 0.0f);
    29.       endPos = newVector3 (4.0f, 4.0f, 0.0f);
    30.  
    31.       xList.Add (theGameObject);
    32.       xList.Add (theGameObject1);
    33.       xList.Add (theGameObject2);
    34.       xList.Add (theGameObject3);
    35.       xList.Add (theGameObject4);
    36.       xList.Add (theGameObject5);
    37.       xList.Add (theGameObject6);
    38.       xList.Add (theGameObject7);
    39.       xList.Add (theGameObject8);
    40.       xList.Add (theGameObject9);
    41.       xList.Add (theGameObject10);
    42.  
    43.       StartCoroutine(FixTimeToRun (rate));
    44.    }
    45.  
    46.    IEnumerator FixTimeToRun(float time2) {
    47.  
    48.       print ("FixTimeToRun");
    49.  
    50.       int s = 0;
    51.  
    52.       while (s < 11) {
    53.          yield return new WaitForSeconds(secRate);
    54.  
    55.          StartCoroutine (MoveTo(xList[s], endPos, rate));
    56.          audio.PlayOneShot(deal);
    57.          s++;
    58.      }
    59.  }
    60.  
    61.  
    62.    IEnumerator MoveTo(GameObject theGO, Vector3 position, float time) {
    63.       print ("MoveTo");
    64.       Vector3 start = theGO.transform.position;
    65.       Vector3 end = position;
    66.       float t = 0;
    67.  
    68.       while(t < 1) {
    69.          yield return null;
    70.          t += Time.deltaTime / time;
    71.          theGO.transform.position = Vector3.Lerp(start, end, t);
    72.        }
    73.       theGO.transform.position = end;
    74.     }
    75. }
    76.  
     
  4. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,353
    For lines 8-18 I suggest using var GameObjectName : GameObject[]. This will allow you to add as many Game object cars as you want. To access them use GameObjectName[index]. Remember numeration starts from 0 not 1.

    This makes things less messy. :)
     
  5. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    I know that very well :)