- Home /
Question by
Tekman03 · Dec 13, 2017 at 01:04 PM ·
c#instantiategetcomponentshooting
Shooting Bullets
I'm sure that this question had been asked loads of times but I have tried many of them and they don't seem to work.
Could someone please help me with what is wrong with these scripts.
Shooter Script:
public GameObject projectilePrefab;
public Transform bulletSpawn;
public Bounds bounds;
public float fireRate = 0.5f;
public float nextFire = 0.5f;
public float speed = 6f;
void Update() {
var move = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
transform.position += move * speed * Time.deltaTime;
if (Input.GetKeyDown("space") && Time.time > nextFire) {
nextFire = Time.time + fireRate;
Instantiate(projectilePrefab, bulletSpawn.position, bulletSpawn.rotation);
}
}
Bullet Script:
public float speed;
void Start () {
GetComponent<Rigidbody2D>().velocity = transform.forward * speed;
}
Comment