Problem with shooting time
Hi, I followed video tutorial (https://www.youtube.com/watch?v=kAx5g9V5bcM) to make my tank gun shoot ,reload for some time ,and can shoot again. But i have big problem...everything work first time , i mean: Player click button to shoot> cannon shoot> player must wait some time until next shoot>player click button again but cannon the ndon't need time to delay :/ //Reload bool reloaded = true; public float reload_time;
void Start ()
{
}
void OnEnable ()
{
reloaded = true;
}
// Update is called once per frame
void Update ()
{
if ((Input.GetKey (KeyCode.Mouse0)) && (reloaded == true)) {
Shooting_Sheels_PzIv_anim.attack ();
//Particle
GameObject Temporary_Particle_Handler;
Temporary_Particle_Handler = Instantiate (Particle, Shells_emmiter.transform.position, Shells_emmiter.transform.rotation) as GameObject;
Temporary_Particle_Handler.transform.Rotate (Vector3.left * 180);
Destroy (Temporary_Particle_Handler, 6.0f);
//The Bullet instantiation happens here.
GameObject Temporary_Bullet_Handler;
Temporary_Bullet_Handler = Instantiate (Shell, Shells_emmiter.transform.position, Shells_emmiter.transform.rotation) as GameObject;
//Sometimes bullets may appear rotated incorrectly due to the way its pivot was set from the original modeling package.
//This is EASILY corrected here, you might have to rotate it from a different axis and or angle based on your particular mesh.
Temporary_Bullet_Handler.transform.Rotate (Vector3.left * 180);
//Retrieve the Rigidbody component from the instantiated Bullet and control it.
Rigidbody Temporary_RigidBody;
Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody> ();
//Tell the bullet to be "pushed" forward by an amount set by Bullet_Forward_Force.
Temporary_RigidBody.AddForce (-transform.up * Shell_Forward_Force);
//Basic Clean Up, set the Bullets to self destruct after 10 Seconds, I am being VERY generous here, normally 3 seconds is plenty.
Destroy (Temporary_Bullet_Handler, 5.0f);
reloaded = false;
}
if (reloaded == false){
StartCoroutine(Reload());
}
}
IEnumerator Reload ()
{
yield return new WaitForSecondsRealtime(reload_time);
reloaded = true;
}
}
here's my code....Somebody have any idea what i do wrong?
If I am understanding you correctly, it is letting you fire over and over without the reload time having any impact. Correct?
If so, I don't see anything obviously wrong in the code you have posted. Can you confirm that there is no reference to the reloaded variable anywhere else in your code, that could be setting it back to true prematurely?
Also, what value are you putting into reload_time?
-Larry
Your answer
Follow this Question
Related Questions
Show ad after certain line of code is called 5 times 1 Answer
Delay Animator Action in C#? 0 Answers
Mining in RTS 0 Answers
Make my moving platforms pause at its endpoints using c# 2 Answers