Search Unity

2 problems when workin with sprites on Unity3D

Discussion in 'Scripting' started by skyx26, Feb 28, 2015.

  1. skyx26

    skyx26

    Joined:
    Dec 30, 2014
    Posts:
    16
    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. publicclassEntra:MonoBehaviour{
    4.  
    5.     publicfloat velocidadDeRotacion =100.0f;
    6.     publicSpriteCartaBase;
    7.     publicSpriteCartaDelantera;
    8.  
    9.     private bool clickActivado =false;
    10.     publicvoidOnMouseDown()
    11.     {
    12.     clickActivado =true;
    13.     }
    14.  
    15.     // Use this for initializationvoidStart(){
    16.     }
    17.  
    18.     // Update is called once per framevoidUpdate()
    19.     {
    20.  
    21.         if(clickActivado)
    22.         {
    23.  
    24.             transform.Rotate(Vector3.up *Time.deltaTime * velocidadDeRotacion);
    25.             if(transform.eulerAngles.y >=90)
    26.             {
    27.  
    28.                 SpriteRendererCarta;
    29.                 Carta=GetComponent<SpriteRenderer>();
    30.                 if(Carta.sprite ==CartaBase)
    31.                 {
    32.  
    33.                     Carta.sprite =CartaDelantera;
    34.                     transform.Rotate(Vector3.up *Time.deltaTime * velocidadDeRotacion);
    35.                     if(transform.eulerAngles.y >=0&& transform.eulerAngles.y <=10)
    36.                     {
    37.  
    38.                         transform.eulerAngles =newVector3(0,0,0);
    39.                         clickActivado =false;return;}
    40.  
    41.                     }
    42.                     clickActivado =false;return;
    43.  
    44.                 }
    45.  
    46.         }
    47.  
    48.     }
    49.  
    50. }
    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
    Code (CSharp):
    1. if (Carta.sprite == CartaBase)

    it jumps stright to

    Code (CSharp):
    1. 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 it live here:
     
    Last edited: Feb 28, 2015
  2. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
  3. skyx26

    skyx26

    Joined:
    Dec 30, 2014
    Posts:
    16
  4. skyx26

    skyx26

    Joined:
    Dec 30, 2014
    Posts:
    16
    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.
     
  5. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    @skyx26

    I just noticed that your posted code does not have a Start() or an Update() method.

    added:
    and it looks like there are some space characters missing
    ie: in bold :-
    1. publicclassEntra:MonoBehaviour{

    2. publicfloat velocidadDeRotacion =100.0f;
    3. publicSpriteCartaBase;
    4. publicSpriteCartaDelantera;

    5. private bool clickActivado =false;
    6. publicvoidOnMouseDown()
     
  6. skyx26

    skyx26

    Joined:
    Dec 30, 2014
    Posts:
    16
    Yes, when I paste the code here it lost format, but on VisualStudio it was properly formatted. Thanks for answering anyway.