- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
Mokhan94 · Oct 24, 2015 at 02:29 AM ·
instantiateshootingbulletinvokeinvokerepeating
Invoke is not working properly.
Hello, I am trying to create a bullet spawn using the invoke function but it is not working. Instead of creating a bullet every 3 seconds, it is waiting 3 seconds and then contionusly creating bullets non stop. Am I not using this function properly.
Also, I am using the MobileSingleStickControl in the unity package as my shooting joystick. I have attached a trigger event on the joystick which is calling the "shoottrig" function below. It is using the PointerDown to set it to true, and PointerUp to set it to false.
Here is the code, please help. Thanks.
var shooting : boolean;
var projectile : Rigidbody;
var speed = 20;
function Update () {
//Face Joystick Direction
var joystickHorizontal = UnityStandardAssets.CrossPlatformInput.CrossPlatformInputManager.GetAxis("Horizontal");
var joystickVertical = UnityStandardAssets.CrossPlatformInput.CrossPlatformInputManager.GetAxis("Vertical");
transform.LookAt(transform.position + Vector3(joystickHorizontal, 0, joystickVertical), -Vector3.forward);
//Shooting triggered
if(shooting){
Invoke("Shooting", 3.0f);
}
}
//Creating shooting functions
function Shooting(){
Debug.Log("we are shooting");
//TESTTTT
var clone : Rigidbody;
clone = Instantiate(projectile, transform.position, transform.rotation);
// Give the cloned object an initial velocity along the current
// object's Z axis
clone.velocity = transform.TransformDirection (Vector3.forward * speed);
}
//Function called through the PointerDown trigger.
function shoottrig(which : boolean){
shooting = which;
}
Comment
You could just use InvokeRepeating in the Start function:
InvokeRepeating("Shooting", 0.0f, 3.0f);
and then in the shooting function do this:
function Shooting(){
if (shooting) {
Debug.Log("we are shooting");
}
}
Great! This is exactly what I was looking for!