- Home /
adding delay to certain part of script.,Adding Delay to part of a script?
so i'm quite new to unity scripting and was trying to add a delay to a gun shooting script i have. its for a crappy vr project and so I managed to make it work with the vive controller but the bullets come out too fast, was wondering if someone could help me IN DETAIL with this, thanks.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SpawnObjectOnButtonPressedScript : MonoBehaviour { public GameObject Bullet_Emitter; public VRTK.VRTK_ControllerEvents controllerEvents; public GameObject Bullet; public float Bullet_Forward_Force;
void Update ()
{
if (Input.GetButton("Fire1") || controllerEvents.triggerPressed)
{
GameObject Temporary_Bullet_Handler;
Temporary_Bullet_Handler = Instantiate(Bullet,Bullet_Emitter.transform.position,Bullet_Emitter.transform.rotation) as GameObject;
Rigidbody Temporary_RigidBody;
Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody>();
Temporary_RigidBody.AddForce(transform.right * Bullet_Forward_Force);
Destroy(Temporary_Bullet_Handler, 3.0f);
}
}
} ,so i have gone through a few forums and yt videos to learn how to make a working gun using instantiate. (im trying to make a simple vr shooter game) it all works fine but the bullets are really fast when shot, so Im trying to add a delay using couratine and ienumerator but I cant figure it out. heres my script with out it and then with it, thanks.
WITHOUT DELAY
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SpawnObjectOnButtonPressedScript : MonoBehaviour { public GameObject Bullet_Emitter; public VRTK.VRTK_ControllerEvents controllerEvents; public GameObject Bullet; public float Bullet_Forward_Force;
void Update ()
{
if (Input.GetButton ("Fire1") || controllerEvents.triggerPressed)
{
StartCoroutine (processTask ());
}
IEnumerator processTask()
{
yield return new WaitForSeconds (0.2);
GameObject Temporary_Bullet_Handler;
Temporary_Bullet_Handler = Instantiate(Bullet,Bullet_Emitter.transform.position,Bullet_Emitter.transform.rotation) as GameObject;
Rigidbody Temporary_RigidBody;
Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody>();
Temporary_RigidBody.AddForce(transform.right * Bullet_Forward_Force);
Destroy(Temporary_Bullet_Handler, 3.0f);
}
}
}
WITH DELAY
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SpawnObjectOnButtonPressedScript : MonoBehaviour { public GameObject Bullet_Emitter; public VRTK.VRTK_ControllerEvents controllerEvents; public GameObject Bullet; public float Bullet_Forward_Force;
void Update ()
{
if (Input.GetButton ("Fire1") || controllerEvents.triggerPressed)
{
StartCoroutine (processTask ());
}
IEnumerator processTask()
{
yield return new WaitForSeconds (0.2);
GameObject Temporary_Bullet_Handler;
Temporary_Bullet_Handler = Instantiate(Bullet,Bullet_Emitter.transform.position,Bullet_Emitter.transform.rotation) as GameObject;
Rigidbody Temporary_RigidBody;
Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody>();
Temporary_RigidBody.AddForce(transform.right * Bullet_Forward_Force);
Destroy(Temporary_Bullet_Handler, 3.0f);
}
}
}
Answer by pako · May 29, 2018 at 11:11 AM
When you say "the bullets are really fast when shot" I assume that you mean that too many bullets get Instantiated, and not that the speed of the bullets is too big. It seems that way in you code.
Also, the code samples you posted WITH & WITHOUT DELAY seems identical to me. So, I assume that the WITHOUT DELAY code is the one without the coroutine.
And you should define the processTask() coroutine outside of Update(), i.e. as a separate method. Does the project even compile like this?
Having said that, the source of the problem is that in both cases you instantiate a bullet in every frame while the "Fire" button is held down. This is not very obvious in the case of the coroutine, which adds the 0.2s delay. In this case however, you are starting a new instance of the coroutine in every frame while the "Fire" button is held down. In general, it's not good practice to start a coroutine inside Update(), because a new coroutine instance is started every frame, i.e multiple coroutines are running, not just one.
The solution: Add a delay directly inside Update, instead of using a coroutine:
public float Delay = 0.2f; //public so that you can set this in the inspector and easily experiment with it.
private float elapsedTime = 10f; //you can set a large initial value, so there's no delay for the first bullet
void Update ()
{
if (Input.GetButton("Fire1") || controllerEvents.triggerPressed)
{
if(elapsedTime > Delay)
{
GameObject Temporary_Bullet_Handler;
Temporary_Bullet_Handler = Instantiate(Bullet,Bullet_Emitter.transform.position,Bullet_Emitter.transform.rotation) as GameObject;
Rigidbody Temporary_RigidBody;
Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody>();
Temporary_RigidBody.AddForce(transform.right * Bullet_Forward_Force);
Destroy(Temporary_Bullet_Handler, 3.0f);
elapsedTime = 0; //reset
}
else
{
elapsedTime += Time.deltaTime; // increase
}
}
}
Answer by hawksandwichgames · May 29, 2018 at 10:57 AM
I think the problem here is that you've defined the IEnumerator inside of another function (void Update in your case.) IEnumerator is a return type of its own, in the same way "void" is. All you have to do is set your IEnumerator outside of any other functions so it stands on its own as a function.
void Update ()
{
if (Input.GetButton ("Fire1") || controllerEvents.triggerPressed)
{
StartCoroutine (processTask ());
}
}
IEnumerator processTask()
{
yield return new WaitForSeconds (0.2);
GameObject Temporary_Bullet_Handler;
Temporary_Bullet_Handler = Instantiate(Bullet,Bullet_Emitter.transform.position,Bullet_Emitter.transform.rotation) as GameObject;
Rigidbody Temporary_RigidBody;
Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody>();
Temporary_RigidBody.AddForce(transform.right * Bullet_Forward_Force);
Destroy(Temporary_Bullet_Handler, 3.0f);
}
Your answer
