- Home /
problem with instantiate function
i am having a problem with instantiating bullet in my project. when i call the fire then it instantiate three bullet at a time. i don't know what is the problem. please someone help me.
the code is:
function Fire () {
// Did the time exceed the reload time?
if (Time.time > reloadTime + lastShot && ammoCount > 0 && count>0) {
count--;
if(count <= 0){
yield WaitForSeconds(2.0);
count = resetcount;
Debug.Log("print");
}
//guitexthint.message = count;
// create a new projectile, use the same position and rotation as the Launcher.
var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
// Give it an initial forward velocity. The direction is along the z-axis of the missile launcher's transform.
instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));
// Ignore collisions between the missile and the character controller
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
lastShot = Time.time;
ammoCount--;
}
}
Answer by Wolfram · Jun 16, 2012 at 01:51 PM
The code you presented will only spawn at most one projectile for each call to Fire(). So either there is a problem in your calling function, calling Fire() more than once, or it is a delayed reaction:
If count is
However, this problem depends on the value of "resetcount" (and the initial value of "count"). If it is 1, one call to Fire() (assuming you have ammo and realoadTime has passed) will be delayed by 2 seconds, then one shot will be fired. Calling Fire() again in the meantime will do nothing. If it is 2, one call to Fire() will fire immediately, and then the same happens as with 1, and so on.
What is your intention of using this counter named "count" and delaying the shot by two seconds?
Answer by vinodkumar · Jun 18, 2012 at 07:11 AM
thanks for your reply . i have set the vales of resetcount and count to 30, so that after every 30 bullet it will wait for 2 seconds. but i don't know why it is firing more than one projectile/bullet on every call to the fire function
Please don't add comments as answers, there is an "add new comment" button for that.
As I told you in my answer, it is not possible for a single call to Fire() to spawn multiple particles. So if that is the effect you see, the problem is in the calling script, i.e., every place where you call Fire(), or maybe you have the script(s) that calls Fire() attached more than once, or attached to multiple objects.