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
1
Question by Poi7ioN · Jun 18, 2019 at 11:20 AM · coroutinesetactivepoolingparticleeffect

Problem with Object Pooling and Deactivating Objects.!!

I've a top down shooter game where the player shoots continuous laser beam , I've achieved this with line renderer and raycast. Everything works well the laser shoots and collides with enemy. But i wanted to try some particle effect at the end of collision i've provided the hyper link to what i'm implying. [link text][1] [1]: https://img.itch.zone/aW1nLzE4NzQzMTAuZ2lm/original/fsRc9m.gif

At the collision i want the particle effect to play. So this is what i've did

  // Update is called once per frame
     void Update()
     {
         _lineRenderer.SetPosition(0, new Vector3(transform.position.x, transform.position.y, transform.position.z));
         RaycastHit2D hit = Physics2D.Raycast(new Vector2(transform.position.x, transform.position.y), transform.up);
         if (hit.collider.tag == "Enemy")
         {
             _lineRenderer.SetPosition(1, new Vector3(hit.point.x, hit.point.y, transform.position.z));
             hit.collider.gameObject.GetComponent<Enemy>().Health -= 1;
             laserHitEffect = ObjectPooler.SharedInstance.GetPooledObject("LaserBeam");
             laserHitEffect.transform.position = hit.point;;
             laserHitEffect.SetActive(true);
             StartCoroutine(DeactivateLaser());
         }
         else
         {
             _lineRenderer.SetPosition(1, transform.up * 2000);
         }
     }
 
     private IEnumerator DeactivateLaser()
     {
         yield return new WaitForSeconds(0.1f);
         laserHitEffect.SetActive(false);
     }

Before using pool i just simply instantiated and destroyed the particle effect object and it worked well the particle effect played at the collision and destroyed in 0.1sec , but instantiating and destroying large number of object again and again is certainly not performance friendly. So i decided to use object pooling.

laserHitEffect = ObjectPooler.SharedInstance.GetPooledObject("LaserBeam");

Here in the inspector "LaserBeam" tagged object are pooled and expanded as per necessary in real time. but my problem is when the laser hit the enemy collider the gameobject gets activated but the Coroutine doesn't disable the gameobject leaving red trails behind which obviously looks ugly. can someone help me with this, or else any other ideas to achieve the above effect is also welcome.

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 Kennai · Jun 18, 2019 at 11:51 AM

Hi, @Poi7ioN! You do not need to activate and deactivate effect each 0.1f. instead of it, activate it once when you hit, and then sync position with hit point. once your laser stop hit or "miss" target - deactivate effect. pooling system is better for objects like bullets. but for lasers its not needed.

      void Awake()
      {
           laserHitEffect = Instantiate(hitEffectPrefab);
           laserHitEffect.SetActive(false);
      }
 
      void OnDisable()
      {
            if(lasetHitEffect.activeSelf)
                 laserHitEffect.SetActive(false);
      }
 
   // Update is called once per frame
      void Update()
      {
          _lineRenderer.SetPosition(0, new Vector3(transform.position.x, transform.position.y, transform.position.z));
          RaycastHit2D hit = Physics2D.Raycast(new Vector2(transform.position.x, transform.position.y), transform.up);
          if (hit.collider.tag == "Enemy")
          {
              _lineRenderer.SetPosition(1, new Vector3(hit.point.x, hit.point.y, transform.position.z));
              hit.collider.gameObject.GetComponent<Enemy>().Health -= 1;
 
              if(!laserHitEffect.activeSelf)//activate effect if not activated
                    laserHitEffect.SetActive(true);
 
              //and update effect position
              laserHitEffect.transform.position = hit.point;
              laserHitEffect.transform.rotation = _lineRenderer.transform.rotation; //your hit effect should be turned "back" in prefab, so when we sync this rotations, you will see effect direction
              //you can remove rotation sync if not needed
          }
          else
          {
              _lineRenderer.SetPosition(1, transform.up * 2000);
              if(laserHitEffect.activeSelf)//deactivate effect if we "missed"
                   laserHitEffect.SetActive(false);
          }
      }
Comment
Add comment · Show 2 · 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 Poi7ioN · Jun 18, 2019 at 01:47 PM 0
Share

Awesome, Worked like a charm...Thanks for your support .:) And also i did saw your $$anonymous$$m project it looks fantastic.

avatar image Kennai Poi7ioN · Jun 18, 2019 at 02:56 PM 0
Share

Haha! Thanks! Feel free to ask any questions and good luck! :D

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

115 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

Related Questions

Coroutine couldn't be started becuase inactive 0 Answers

Why SetActive Game Objects generate too much Garbage in the profiler? 1 Answer

physics.OverlapSphere colliders 1 Answer

Using an object pool with Javascript. 1 Answer

Coroutine only starts when directly loading a scene from Unity editor. 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