2
\$\begingroup\$

I need to replace the sprite of a face down card for another sprite (of a face up card).

This is the C# script attached to the face down card:

using UnityEngine;
using System.Collections;

public class Entra : MonoBehaviour
{

    public float velocidadDeRotacion = 100.0f;
    public Sprite CartaBase;
    public Sprite CartaDelantera;

    private bool clickActivado = false;
    public void OnMouseDown()
    {
        clickActivado = true;        
    }

    // Use this for initialization
    void Start ()
    {

    }

    // Update is called once per frame
    void Update ()
    {

        if (clickActivado)
        {
            transform.Rotate(Vector3.up * Time.deltaTime * velocidadDeRotacion);
            if (transform.eulerAngles.y >= 90)
            {
                SpriteRenderer Carta;
                Carta = GetComponent<SpriteRenderer>();
                if (Carta.sprite == CartaBase)
                {
                    Carta.sprite = CartaDelantera;
                    transform.Rotate(Vector3.up * Time.deltaTime * velocidadDeRotacion);
                    if (transform.eulerAngles.y >= 0 && transform.eulerAngles.y <=10)
                    {
                        transform.eulerAngles = new Vector3(0, 0, 0);
                        clickActivado = false;
                        return;
                    }

                }
                clickActivado = false;
                return;
            }

        }

    }
}

As you can see, I rotate the card when I click on it, when the angle reach 90º I stop the rotation and change the sprite, from CartaBase to CartaDelantera.

Right now I have two problems.

  1. When the sprite has an angle of 1º or more, the image breaks. I lose the right half of the image from the center to the right border, and I don't understand why.
  2. Using VS2013 as debugger, I make a step by step walkthrough and when it reach

    if (Carta.sprite == CartaBase)

it jumps stright to

clickActivado = false;

So, obviously it's not "seeing" the sprite used by the card. Why???

I'm using this script as reference for the change of sprites.

If you don't understand my problem, you can see live here.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

I solve the second problem. It was in front of me all the time.

As I said, I was using this code as example. What this awesome answer doesn't explain you is that once you compile the code, if you select the sprite back on hierchy you'll notice two variables available, corresponding to the one you declare back in the code.

My BIG mistake was not understanding the folowing, in the example @Savlon said: "// Drag your first sprite here" wich I literally understood as "drag your sprite to the VS". Yes, I know, I'm a huge dumbass, please don't say it...

What I needed to do was draging the sprites to THE INSPECTOR, back on Unity3D. More specifically, to the new variable available on the sprite.

I also solve the first problem by setting the camera as many units on the Z axis as wide was the sprite. The problem was that the camera was too damn close to the sprite, so when the sprite turns it was behind the camera and therefore that part of the sprite was not rendered. The original suggestion of @Nosekills point me to the right direction.

Thanks to @Nosekill and @hexagonius for the help supplied.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .