- Home /
The question is answered, right answer was accepted
Invoke shoot delay problem.
I can't figure out how to delay my gun shoots/shooting/shots.... To delay between the shots.
I'm tryong to use Invoke("Firing", shotDelay); and its not working.
For the AI I use InvokeRepeating and thats making it even worse lol.
This is the code in thee gun.
public float shotDelay = 1.0f;
void Firing(){
Instantiate...bla bla, it works.
}
// this what is invoked by the AI which then invokes the above with delay, which is not working.
void Shoot()
Invoke("Firing", shotDelay);
}
}
... and this is inside the AI script
void Shooting(){
weaponRifleScript.InvokeRepeating("Shoot", 0.5f, 20);
// The invoke repeatings last float 20 which is invoke rate seems to have no effect.
}
Same problem with the player controlled Rifle too.
//
void Update(){
if(Input.GetKeyDown(KeyCode.Mouse01))
{
Invoke("Firing", shotDelay)
}
}
To clarify, the Invoke delay in the rifle scripts is not working. No delay is there. How do I get a good delay?
Please help me...
Thanks.
Are you trying to add a delay between shots, or add a delay between someone pressing the fire button & the weapon firing ?
Adding a delay between shots can be implemented thusly:
public class Weapon : $$anonymous$$onoBehaviour
{
public double RechargeTime = 0;
private double NextFireTime = 0;
public Awake()
{
NextFireTime = Time.time;
}
public bool HasWeaponRecharged()
{
return (Time.time > NextFireTime);
}
public void Update()
{
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0)&& HasWeaponRecharged())
FireWeapon();
}
public void FireWeapon()
{
// Instantiate......
endDelay = Time.time + shotDelay;
}
}
Now the bullet is not co$$anonymous$$g out?
// endDelay here is NextFireTime.
public double shotDelay = 10;
public double endDelay = 0;
// is it supposed to be onlu public Awake? because it gives error unless I put public void.
public void Awake(){
endDelay = Time.time;
}
public bool Recharged(){
return (Time.time > endDelay);
}
public void Shoot()
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0)&& Recharged())
{
Instantiate......
endDelay = Time.time + shotDelay;
}
It works if I put public void Shoot as Update but then the delay does not work again.
Answer by Sajidfarooq · Aug 14, 2013 at 09:37 PM
I've modified Weenchehawk's code slightly. The following code is tested, and works.
public double shotDelay = 10;
private double endDelay = 0;
private int shotNumber = 0;
// Yes, it is supposed to be public
public void Awake()
{
endDelay = Time.time;
}
public bool Recharged()
{
return (Time.time > endDelay);
}
public void Shoot()
{
if(Input.GetKeyDown(KeyCode.Mouse0)&& Recharged())
{
Debug.Log ("Shooting bullet " + shotNumber.ToString());
endDelay = Time.time + shotDelay;
shotNumber++;
}
}
public void Update()
{
Shoot();
}
Something like that should do it, Deadkenny, if it works you might like to credit SajidFarooq / myself with an answer.
Last question though, which code is more efficient, Igors or yours?
There is no difference in efficiency. $$anonymous$$ine is more readable which is good in the long run when you want to modify your code later. Igor's more compact.
$$anonymous$$indly do mark this question as answered when you decide the answer you prefer. Closing the thread, and thumbs-up doesn't automatically mark the question as answered.
Answer by IgorAherne · Aug 14, 2013 at 08:15 PM
Example for player controlled Rifle:
float currTime = 0;
void Update(){
if(Input.GetKeyDown(KeyCode.Mouse01) && currTime + shootDelay < Time.time)
{
Invoke("Firing", shotDelay);
currTime = Time.time;
}
Please check my thread, I will be very thankful :) http://forum.unity3d.com/threads/191923-Full-corse-how-to-make-a-horror-game-AVAILABLE
yeah, I've forgot 1 more line in Update. Should be working now
Answer by weenchehawk · Aug 16, 2013 at 10:05 PM
You know, it's funny. I answered this question in another thread on a very similar topic just last night (here : link text). The short version is that most compilers will optimize the sh1t out of most of your statements way better than you ever can. Human's are better at optimizing algorithems though.
I daresay the compiled code in both is probably equally efficient but even if it weren't there wouldn't be much in it. Go with whatever is most readable / maintainable for yourself.
Cool. Ok well this worked out.
You guys answer pretty quick. Glad I got this engine.
Follow this Question
Related Questions
Reload Ammo is not working 0 Answers
Reload Ammo is not working 1 Answer
Multiple Cars not working 1 Answer
adding a delay to shooting 2 Answers