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 Ilkzz · Apr 02, 2014 at 07:07 PM · 2dtimespeedyieldienumerator

Increase variable for a few seconds

Hi,

I want to increase the speed of my character by 5 (in this example) when I collide with my power up and then return back to normal after a set time.

Now, this works but as I want it to.

The way it works now is... I collide, speed goes up but the game object is still there. It is only deleted once the yield is finished. I heard I cannot delete the object until the process is over other it doesn't work.

Its not really a power up if it isn't removed from the scene.

To get around this, I removed the Sprite Rendered. The object becomes invisible which works I guess. But now, during the process, if i touch this invisible object, the speed increase as many times as I touch it till it is deleted...

Here is my code:

 void OnCollisionEnter2D(Collision2D other)
     {
         if (other.gameObject.tag == "Player")
         {
 
             PlayerMovement.movespeed += 5;
             StartCoroutine(speedTime());
             GetComponent<SpriteRenderer>().enabled = false;
         }
     }
     
     IEnumerator speedTime ()
     {
         yield return new WaitForSeconds(3);
         revertSpeed();
         Destroy(gameObject);
 
 
     }
     
     void revertSpeed ()
     {
         PlayerMovement.movespeed = 2;
     }

What is the best way to get around this?

Thanks

Comment
Add comment · Show 1
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 Addyarb · Apr 02, 2014 at 07:13 PM 0
Share

Can you try on line 3:

 if (other.gameObject.tag == "Player" && gameObject.transform enabled = true)

That way you only receive the power-up if the object is visible. Not exactly destroyed but close enough.

Sorry if my syntax is bad, hope you get the idea.

2 Replies

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

Answer by fafase · Apr 02, 2014 at 07:10 PM

I guess if you just remove any visual and physical interaction, you can keep your object there until destruction

 void OnCollisionEnter2D(Collision2D other)
 {
    if (other.gameObject.tag == "Player")
    {
      renderer.enabled = false;    // Remove visual
      collider.enabled = false;    // Remove physical
      PlayerMovement.movespeed += 5;
      StartCoroutine(speedTime());
      GetComponent<SpriteRenderer>().enabled = false;
    }
 }
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 Ilkzz · Apr 02, 2014 at 07:18 PM 0
Share

I didn't know I can remove colliders like that. This definitely helped. Thanks a lot

avatar image
0

Answer by Grim_Darknight · Apr 02, 2014 at 07:25 PM

the easiest way I can think of is to have a bool variable that is off initially and then turned on after collision, it works for what you want and you can reenable it for respawning power-ups.

 private bool tripped = false;
 
 void OnCollisionEnter2D(Collision2D other)
     {
        if (other.gameObject.tag == "Player" && !tripped)
        {
          tripped = true; // this stops OnCollisionEn.... from running again
          PlayerMovement.movespeed += 5;
          StartCoroutine(speedTime());
          GetComponent<SpriteRenderer>().enabled = false;
        }
     }
  
     IEnumerator speedTime ()
     {
        yield return new WaitForSeconds(3);
        revertSpeed();
        Destroy(gameObject);
  
  
     }
  
     void revertSpeed ()
     {
        PlayerMovement.movespeed = 2;
     }`
 
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 fafase · Apr 02, 2014 at 07:37 PM 0
Share

But the collider is still on so your guy is about to bump on it. Actually he should use Trigger.

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

22 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

Related Questions

Boost Script Time problems 1 Answer

Objects moving faster after time 1 Answer

Smoothed Rotation with 2D Top-Down View 1 Answer

How to increase rate of a falling object in pooling object? 1 Answer

What am I doing wrong with IEnumerator? 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