- Home /
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;
}
}
}
}
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.
I never thought of this. Every time you answer one of my questions, all I can imagine is this:
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!!!
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.
tks I see. Have a nice day. I've learned anythings from this community :D
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
Your answer

Follow this Question
Related Questions
Gun Shooting Animation Won't Play 0 Answers
Best way for setting up shooting mechanics for an FPS? (iPhone) 1 Answer
my audio is not looping even when it is set to loop 5 Answers
Advanced FPS shooting 1 Answer
Mecanim shooting 1 Answer