- Home /
Issue with Timed Shooting in C#
I am trying to make a script for a tower like object that will fire a bullet evry set amount of time and I can seem to get the instantiate class function to work with me right now so if someone could help me shoot a bullet from a spawnpoint every set number of seconds, that would be WONDERFUL!!!!!!! it would be even better if you could help me useing C#...
Should we write this code for you from scratch or do you have something we can work from?
Answer by fireDude67 · Feb 01, 2011 at 01:37 AM
using UnityEngine; using System.Collections;
public class example : MonoBehaviour { public GameObject projectile; public float fireRate = 0.5F; private float nextFire = 0.0F; void Update() { if (Input.GetButton("Fire1") && Time.time > nextFire) { nextFire = Time.time + fireRate; GameObject clone = Instantiate(projectile, transform.position, transform.rotation) as GameObject; // Instantiates a projectile facing forward relative to the current forward vector } } }
You need to modify the projectile variable, and apply a ConstantForce
or something to make it move forward
Credit: I took it from the Unity Script Reference
Your answer
![](https://koobas.hobune.stream/wayback/20220613102121im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Bullet stops when new bullet instantiates pls help 3 Answers
take collision info from an instantiated bullet 1 Answer
Timer to call Instantiate 1 Answer
Slow automatic shooting script? 2 Answers
Pass parameters to an instance 1 Answer