Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by skyx26 · Feb 28, 2015 at 09:00 AM · c#2dspriteunity 4.6

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.

  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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hexagonius · Feb 28, 2015 at 09:08 AM 0
Share
  1. Is not clear enough, could you show a screenshot?

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

avatar image skyx26 · Feb 28, 2015 at 06:22 PM 0
Share

@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

avatar image hexagonius · Feb 28, 2015 at 09:56 PM 0
Share

The if block is only executed once because it checks for the image which changed in there.

avatar image skyx26 · Feb 28, 2015 at 10:09 PM 0
Share

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 image

cardname.png (201.0 kB)

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Recoloring animated sprites [Solved] 2 Answers

Rotate the weapon sprite with a joystick 0 Answers

How to break a sprite into shapes with script 1 Answer

Fake 2d object carousel with swipe 2 Answers

How can I cycle through sprites on button presses (C#)? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges