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 b2hinkle · Jul 13, 2018 at 10:26 PM · collisioncoroutineoptimizationshootingkill

Will calling a Coroutine every time the player shoots an object a bad way to program?

I am making a game kind of like Call Of Duty Zombies. I have it so that if the player shoots the zombie with a bullet, it only gives them 10 points, but if the player kills a zombie with a bullet, it gives them 60 points. Here is the code I made for that, and I am wondering if it is an efficient way of doing it, because idk if Coroutines are expensive. If I go this route, then this Coroutine will run every time the player shoots an object.

(just to clarify the coding a bit, when a zombie is alive, it is tagged with "Enemy", and when they die they are tagged with "Untagged").

alt text

capture.png (13.2 kB)
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

Answer by luiscmendez · Jul 13, 2018 at 11:13 PM

Generally yes. But it can get pretty messy if you have multiple coroutines running without ever stopping them. Using a Coroutine is a lot better than using the Update loop. But It's good practice to keep track of a Coroutine so that you can start it and stop it at any given time without creating a new one. In your case you may also want to cache your WaitForSeconds so that it's not creating a new one every single time you run it.

It might be better if you use the Coroutine like an Update loop and leave it running from the start. Then all your logic checks happen within that loop rather than starting the loop every time the player shoots. So you would essentially just keep checking whether or not the player has shot, then run your logic based off of that value rather than running a Coroutine after the player has shot.

Here's a quick example:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.Events;
 
 public class AwardPoints : MonoBehaviour
 {
     public float delayTime = 0.1f;
 
     private IEnumerator currentProcess;
     private WaitForSeconds cachedDelay;
 
     private void Start()
     {
         cachedDelay = new WaitForSeconds(delayTime);
     }
 
     private void StartCoroutine()
     {
         if (currentProcess != null)
         {
             StopCoroutine(currentProcess);
         }
         currentProcess = Process();
         StartCoroutine(currentProcess);
     }
 
     private void StopCoroutine()
     {
         if (currentProcess != null)
         {
             StopCoroutine(currentProcess);
             currentProcess = null;
         }
     }
 
     private IEnumerator Process()
     {
         while (true)
         {
             if(hit.collider.transform.root.tag.Contains("Enemy"))
             {
                 yield return cachedDelay;
                 if(hit.collider.transform.root.tag.Contains("Untagged"))
                 {
                     //Do stuff
                 }
                 else
                 {
                     //Do other stuff
                 }
             }
             yield return null;
         }
     }
 }

So every time you want to run this check you call StartCoroutine() which will stop the current process if it's still running, and start a new one after that. Or just start a new one if one does not exist. Let me know if anything isn't clear.

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 b2hinkle · Jul 14, 2018 at 12:11 AM 0
Share

It seems to work except that when I shoot the zombie with one bullet, it does give me 10 points, but it keeps giving me 10 points until I kill the zombie.

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

150 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

Related Questions

Optimization of bullet hit calculation per frame 1 Answer

How to read particle information from a OnParticleCollision call ? 0 Answers

Using Coroutines and OnGui Together 0 Answers

StartCoroutine for each object in a foreach() Loop 1 Answer

When I kill one enemy, they all die? 0 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