- Home /
Wait for reload to finish?
I have a script which covers most of my weapons shooting and reloading, but I have this problem when you press r you dont have to wait for the reload animation to finish instead you can press r and start shooting straight away. How would I alter this script so that you have to wait a few seconds before you can start shooting again?
this is the script,
var amountOfShots = 8;
function Update (){
if(Input.GetButtonDown("Fire1")){
Shoot();
}
if(Input.GetKeyDown("r")){
amountOfShots = 8;
}
}
var shootSound : AudioClip;
var bloodPrefab : Transform;
var sparksPrefab : Transform;
var hit : RaycastHit;
var range = 500;
function Shoot (){
if(amountOfShots > 0){
amountOfShots--;
if(shootSound){
audio.PlayOneShot(shootSound);
}
if(Physics.Raycast(transform.position, transform.forward, hit, range)){
var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);
if(hit.collider.tag == "Enemy"){
if(bloodPrefab){
Instantiate(bloodPrefab, hit.point, rot);
}
hit.collider.gameObject.SendMessage("ApplyDamage", 25, SendMessageOptions.DontRequireReceiver);
}else{
if(sparksPrefab){
Instantiate(sparksPrefab, hit.point, rot);
}
}
}
}
}
Answer by FLASHDENMARK · Dec 04, 2011 at 01:09 PM
var reloadTime = 2.5;
yield WaitForSeconds(reloadTime); // Waits
You should really search before you ask these questions they have already been answered.
http://unity3d.com/support/documentation/ScriptReference/WaitForSeconds.html
EDIT: Update cannot be coroutined you will have to do your reload stuff in a new function:
var reloadTime = 2.5;
function Update (){
if(Input.GetKeyDown("r")){
Reload(); //Calls the reload function
}
}
function Reload (){
yield WaitForSeconds(reloadTime);
amountOfShots = 8;
}
Now it should work. I am really sorry that I forgot to tell you about this. Now wonder it does not work.
Good luck.
Come on, this is not really about scripting anymore, now it is more of a logical issue. Brake the problems into simple and logical portions. You answered your question yourself by saying: "How would I alter this script so that you have to wait a few seconds before you can shoot again.
Try thinking a little about the problem. Trail and error is also one of the best ways of learning; you try something, it doesn't work, you try again, repeat, profit.
Good luck.
Im sorry for being a pain ive taken your advise and tried over and over again but im just terrible at scripting, I know this way would be better for me to learn but I cant figure it out it either dosent work or I get an error saying "Update() can not be a coroutine" please can you help me?
Oh, damn I totally forgot to tell you that sorry.
In Update()(Including other function) you cannot use yield WaitForSeconds you will need to define a new function for that to work. I will update my post.
Thanks for all your help you've been very helpful! thanks again!
Your answer
Follow this Question
Related Questions
Reloading Help 2 Answers
Reload Tutorial? 0 Answers
Blender animation 1 Answer
adding animation dynamically by script 0 Answers
Animation Script Help 0 Answers