- Home /
2 problems when workin with sprites on Unity3D
Hi guys. 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 = newVector3(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.
Is not clear enough, could you show a screenshot?
There's no "clickActivado =false;" outside the if statement and inside it's definitely called. Please clarify what exactly the problem is. What do you mean by "seeing"? You're not checking any visibility things. By that, check the scene view and see of the card just stays at a nearly perfect 90° angle to the camera
@Nose$$anonymous$$ills I fix the code syntax. The original idea was click the card, then it rotate 90º, then I change the sprite and rotate the card 90º more, like I was flipping the card. @hexagonius You can see the full problem in the following video
The if block is only executed once because it checks for the image which changed in there.
Yes, I already know that @hexagonius. I check if the name of the sprite I'm using is CartaBase, wich is. If the name of the sprite in use match CartaBase then blah, blah, blah. The problem is, the code doesn't recognise the name of the sprite as equal, so the IF condition never fullfills and jumps to clickActivado =false; and it should match (should be equal, ==) because it's the name of the sprite on use, as you can check in the
Answer by skyx26 · Mar 01, 2015 at 03:13 AM
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.