Seongwon-Choi
Seongwon-Choi

Reputation: 457

How to change Sprite Image when it reaches 90 degrees?

I say that I am a beginner.

I have a question during the project.

I'm currently implementing a card-matching game.

When I start the game, The image is loaded from the external path (D: / ..).

My question is in the code below.

 public void createCardList()
        {
            int count_x = 0;
            int count_y = 0;

            GameObject parent_area = GameObject.Find("GameMain");
            List<int> num = createRandomIndex(createArrayIndex());

            for (int i = 0; i < card_list.Count; ++i)
            {
                if ((i % CARD_ROWS) == 0 && i > 0)
                {
                    count_y += 1;
                    count_x = 0;
                }

                GameObject card_prefab_load = Resources.Load("Prefabs/card") as GameObject;
                GameObject card_prefab_instantiate = Instantiate(card_prefab_load, card_prefab_load.transform.position, Quaternion.identity);

                float card_position_x = (CARD_WIDTH + CARD_SPACE) * (i % CARD_ROWS) - 290f;
                //Debug.Log("card_position_x" + card_position_x);
                float card_position_y = count_y * (CARD_HEIGHT + CARD_SPACE) - 280f;
                //Debug.Log("card_position_y" + card_position_y);

                card_prefab_instantiate.transform.SetParent(parent_area.transform);
                card_prefab_instantiate.transform.name = "card_" + num[i];
                card_prefab_instantiate.transform.localScale = new Vector3(1f, 1f, 1f);
                card_prefab_instantiate.transform.localPosition = new Vector3(card_position_x, card_position_y, 1f);

                StartCoroutine(firstRotateOriginalImage());
            }
        }

        // Rotate image
        private IEnumerator firstRotateOriginalImage()
        {
            yield return new WaitForSeconds(2f);

            GameObject findCardList = GameObject.Find("GameMain");

            for (int i = 0; i < findCardList.transform.childCount; ++i)
            {
                // I don't know what code to put in this part.
            }
        }

What I want to implement is below.

  1. When the card rotation value reaches 90 degrees,How to change an externally imported image to a Sprite Image of a child GameObject?

  2. How to rotate the child objects 360 degrees after the first task is completed?

For example, the picture below. enter image description here

Arrows indicate the order in which cards are flipped. also, Of the 12 GameObjects, Six GameObjects try to implement a common image.

I don't know much. I need your help.

Upvotes: 0

Views: 254

Answers (2)

MadStark
MadStark

Reputation: 585

I'm a doubtful regarding how well I understand your question... But it seems like you want to simply flip the cards over don't you?

The approach I'd take is to have each card as the combination of the face (rotated 180 degrees in Y) and the back, both being children of an empty GameObject.

That way you could simply rotate the Card object using transform.Rotate(0, 180, 0)

To use it in a coroutine you could do

//Speed of animation (here 0.55 seconds)
float degreesPerSecond = 200f;

//The amount you want to rotate
float maxDegrees = 180f;

//Animate
for (float f = maxDegrees; f < 0;)
{
    //How much to rotate
    float rot = degreesPerSecond * Time.deltaTime;

    //Rotate children
    foreach(var child in transform)
        child.Rotate(0, rot, 0);

    //Record rotation
    f -= rot;

    //Wait next frame
    yield return null;
}

//Round to desired rotation
foreach(var child in transform)
        child.position = new Vector3(child.position.x, maxDegrees, child.position.z);

Upvotes: 0

CosmicGiant
CosmicGiant

Reputation: 6439

There are many ways to do that...

  1. transform.Rotate(...)
  2. transform.RotateAround(...)
  3. transform.localEulerAngles = transform.localEulerAngles.X|Y|Z + amount
  4. transform.localRotation = transform.localRotation*relativeRotation /*= a Quaternion*/
  5. Something else entirely...

Upvotes: 0

Related Questions