Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 farazk86 · Oct 19, 2017 at 08:16 PM · unity 52dspriteunity 2dspriterenderer

How to change sprite only on click or during shoot state?

So I have a walking/idle sprite and a shooting sprite. When I call the shoot function I want the sprite to change to the shooting one but after it has finished shooting go back to the idle sprite.

This is what I have come up with:

 private Sprite[] shootSprite;
     
 
     // Use this for initialization
     void Start()
     {
         
         shootSprite = Resources.LoadAll<Sprite>("spritesheet_characters");
     }
 
     // Update is called once per frame
     void Update()
     {
         if (fireRate == 0)      //if single burst fire
         {
             if (Input.GetMouseButtonDown(0))       //set Fire1 in input manager
             {
                 Shoot();
             }
         }
         else
         {
             if (Input.GetMouseButtonDown(0) && Time.time > timeToFire)
             {
                 timeToFire = Time.time + 1 / fireRate;
                 Shoot();
             }
         }
     }

  void Shoot()
     {
         gameObject.GetComponent<SpriteRenderer>().sprite = shootSprite[15];
         //rest of code related to shooting.
 }

Now the problem is that once I click it goes to shooting sprite and then stays there, it should go back to previous sprite.

I tried putting:

 gameObject.GetComponent<SpriteRenderer>().sprite = shootSprite[39]; //idle sprite

within Update() but that does not work well as the transition is very fast and very rarely do I see the sprite change during shooting.

Also I dont want to be changing sprite every update as I am using an animator to rotate it while walking.

Is there any way to do this?

Thanks

Comment
Add comment
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

1 Reply

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

Answer by Ratslayer · Oct 19, 2017 at 10:01 PM

You have to put the idle sprite line in an else at the end of every if that has Shoot().

 if (fireRate == 0)      //if single burst fire
          {
              if (Input.GetMouseButtonDown(0))       //set Fire1 in input manager
              {
                  Shoot();
              }
              else 
              {
                   gameObject.GetComponent<SpriteRenderer>().sprite = shootSprite[39]; //idle sprite
              }
          }
          else
          {
              if (Input.GetMouseButtonDown(0) && Time.time > timeToFire)
              {
                  timeToFire = Time.time + 1 / fireRate;
                  Shoot();
              }
              else
              {
                   gameObject.GetComponent<SpriteRenderer>().sprite = shootSprite[39]; //idle sprite
              }
          }

Also, you need to refactor your if/elses, there is way too many of them. I don't see a reason for you to call Shoot() more than once in your code, IMHO.

And I would look into mechanim, stuff like changing sprite states is better handled by state machines.

Comment
Add comment · Show 1 · 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
avatar image farazk86 · Oct 20, 2017 at 10:38 PM 0
Share

Thank you, and yes you are right it is much easier to do this using the mechanim. Thats how I ended up going with

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

209 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity2D Sprite Fading Issues 1 Answer

convert unity versions, 0 Answers

Change sprite at runtime 1 Answer

check the presence of desired sprite in canvas on click? 0 Answers

How can i change the sprite image of button in run time of a 2d game 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