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.
- 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.
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.