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 /
  • Help Room /
avatar image
0
Question by OnandiaN · Dec 14, 2016 at 05:46 AM · spriteunity 2dfade

Unity 2D Sprite Fade In/Out OnTriggerEnter2D

Hello!

First I'd like to establish I'm very VERY new to coding. This being said, I'm currently messing around with the 2D engine, and what I want to happen is have the player enter a trigger volume and when they do a sprite fades to a completely transparent alpha, and then when they exit the trigger volume it goes back to a 100% alpha.

I've found some codes that I was able to somewhat modify to the task, however in one way or another they don't exactly meet what I want. I've been trying to modify a particular code to no avail, however in its untouched state it does exactly what I want except on a clock instead of when they player enters/exits a trigger.

When I change it to OnTriggerEnter2D instead of start function, it triggers the clock. Then I tried making a boolean variable that would make it so the coroutine would run only once, however that didn't work and it looped no matter what I tried. Then I tried getting rid of the coroutine and just making the OnTriggerEnter function run the code, however because it's using yield return I have to have it in an IEnumerator which I'm not sure how to trigger with a function. I've also tried looking into using the actual SpriteRenderer component, however from my research searching through UnityAnswers they said it's apparently unstable to change it with that.

What would you recommend I do? Here's the original code I've been trying to modify, courtesy of another Unity Answers post.

 using UnityEngine;
  using System.Collections;
  
  public class Fade : MonoBehaviour
  {
      public SpriteRenderer sprite;
  
      public Color spriteColor = Color.white;
      public float fadeInTime = 1.5f;
      public float fadeOutTime = 3f;
      public float delayToFadeOut = 5f;
      public float delayToFadeIn = 5f;
  
      void Start()
      {
          StartCoroutine("FadeCycle");
      }
  
      IEnumerator FadeCycle()
      {
          float fade = 0f;
          float startTime;
          while(true)
          {
             startTime = Time.time;
             while(fade < 1f)
             {
                 fade = Mathf.Lerp(0f, 1f, (Time.time - startTime) / fadeInTime);
                 spriteColor.a = fade;
                 sprite.color = spriteColor;
                 yield return null;
                 print ("Step 1");
             }
              //Make sure it's set to exactly 1f
              fade = 1f;
              spriteColor.a = fade;
              sprite.color = spriteColor;
              yield return new WaitForSeconds(delayToFadeOut);
             print ("Step 2");
  
              startTime = Time.time;
              while(fade > 0f)
              {
                  fade = Mathf.Lerp(1f, 0f, (Time.time - startTime) / fadeOutTime);
                  spriteColor.a = fade;
                  sprite.color = spriteColor;
                  yield return null;
                 print ("Step 3");
              }
              fade = 0f;
              spriteColor.a = fade;
              sprite.color = spriteColor;
              yield return new WaitForSeconds(delayToFadeIn);
             print ("Step 4");
          }
      }
  }
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

0 Replies

· Add your reply
  • Sort: 

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

103 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

Related Questions

Holding Alt while placing object created phantom duplicates 0 Answers

Cycling through array not working. Pulling my hair over this one. 0 Answers

> Sprite outline generation failed - could not read texture pixel data. Did you forget to make the texture readable? 0 Answers

creating a 2d peeling system in unity 0 Answers

Calculate BoxCollider2D based on the actual player sprite 2 Answers


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