Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 connorwforman · Jan 03, 2018 at 11:33 PM · fpsshootingfreeze

Unity freezes on play when I left click.

I had some help slowing down the ROF (rate of fire) on a gun, but when I go to shoot, unity freezes, and doesn't come back. It happened after writing the script, so I am guessing it is a scripting problem. Here is the script:

 public float delay = 0.5f;
  public float timeLeft = 0.5f;
 
 void Shoot () {
         while (isFullAuto == true) {
             if (CurrentAmmo <= 0.5)
                 break; 
             RaycastHit hit; 
             if (Physics.Raycast (WeaponCamera.transform.position, WeaponCamera.transform.forward, out hit, range)) {
                 Debug.Log (hit.transform.name); 
 
                 health = hit.transform.GetComponent<EnemyHealth> (); 
                 if (health != null) {
                     health.TakeDamage (damage); 
                 }
 
                 if (hit.rigidbody != null) {
                     hit.rigidbody.AddForce (-hit.normal * hitForce); 
                 }
                 if (timeLeft > delay) {
                     GameObject impactGO = Instantiate (impactEffect, hit.point, Quaternion.LookRotation (hit.normal)); 
                     Destroy (impactGO, 2f);
                 
                     CurrentAmmo -= 1; 
                     shotSound.Play ();
                     timeLeft = delay;
             }
         }
     }
 }

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

2 Replies

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

Answer by MaxGuernseyIII · Jan 04, 2018 at 06:41 PM

@JVLVince has provided a good fix within your design. You might want to reconsider it, though. It seems like a coroutine for shooting might be a little more natural to the problem you are trying to solve, the way you want to express your solution, and the constraints imposed by Unity.

Essentially, coroutines are cooperative threads. They use the enumeration mechanism built into C# and oh-so-cleverly coerce the "yield" keyword to mean "yield control from this thread". Because of this design, you can do things like yield for a specific amount of time. So, with a coroutine, you could basically use your original algorithm and just yield an half-second delay after you shoot each bullet.

Further reading: Coroutines.

Comment
Add comment · Show 5 · 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 connorwforman · Jan 04, 2018 at 08:36 PM 1
Share

I never thought of this. Every time you answer one of my questions, all I can imagine is this:

alt text

avatar image MaxGuernseyIII connorwforman · Jan 04, 2018 at 08:55 PM 0
Share

LOL

Thanks.

avatar image JVLVince · Jan 05, 2018 at 04:21 AM 1
Share

100% agree with your opinion. I considered use coroutines but I’m not sure if it’s will change his design, since I’m still too fresh lol so I just point out some basic problems of his solution. Tks for your answer. Cheers!!!

avatar image MaxGuernseyIII JVLVince · Jan 05, 2018 at 05:00 AM 0
Share

I suspected you were trying to work within the proposed design and respect that. I do that a lot of the time for different reasons than the one you mentioned. Just remember that you can never hurt anyone by giving design advice that was meant to help.

avatar image JVLVince MaxGuernseyIII · Jan 08, 2018 at 12:44 PM 0
Share

tks I see. Have a nice day. I've learned anythings from this community :D

avatar image
1

Answer by JVLVince · Jan 04, 2018 at 02:51 AM

Where you call this Shoot() Method (In Update)? And are you sure your CurrentAmmo var is only change inside this while clause?
As I know since this Shoot() method called, your game will freeze for awhile until while clause breaks. So in my opinion your code should change to this (this just an example, I don't know where you define CurrentAmmo var, as the naming it's seem like properties so I let you define it yourself ) then leftclick mouse down, set the isShooting to true, mouse up, set the isShooting to false can solve your problem( I think) :

 public class HandleShoot : MonoBehaviour{
     public bool isShooting;
     public float delay = 0.5f;
     public float timeLeft = 0.5f;
 
     void Update(){
         if (isShooting){
             Shoot();
         }
     }
  
     void Shoot () {
         if (!isFullAuto)
             return;
         if (CurrentAmmo <= 0.5)
             return;
         RaycastHit hit; 
         if (Physics.Raycast (WeaponCamera.transform.position, WeaponCamera.transform.forward, out hit, range)) {
             Debug.Log (hit.transform.name); 
  
             health = hit.transform.GetComponent<EnemyHealth> (); 
             if (health != null) {
                 health.TakeDamage (damage); 
             }
  
             if (hit.rigidbody != null) {
                 hit.rigidbody.AddForce (-hit.normal * hitForce); 
             }
             if (timeLeft > delay) {
                 GameObject impactGO = Instantiate (impactEffect, hit.point, Quaternion.LookRotation (hit.normal)); 
                 Destroy (impactGO, 2f);
                  
                 CurrentAmmo -= 1; 
                 shotSound.Play ();
                 timeLeft = delay;
             }
         }
     }
 }

Hope this help, Cheers !!! Vince

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 connorwforman · Jan 04, 2018 at 06:18 PM 0
Share

I will test it now.

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

102 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

Related Questions

Enemy Will not die 1 Answer

fps shooting in the direction of character main cam 1 Answer

How do I destroy a gameobject after 5 seconds 2 Answers

How can I change my script to destroy a GameObject on hit? 2 Answers

Wrong Shoot Animation 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