Skip to content

Instantly share code, notes, and snippets.

@didacus
Created November 14, 2021 15:23
Show Gist options
  • Save didacus/01f50a7f763f525f3040f2bf10a208ae to your computer and use it in GitHub Desktop.
Save didacus/01f50a7f763f525f3040f2bf10a208ae to your computer and use it in GitHub Desktop.
Unity - Basic gun controller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GunController : MonoBehaviour
{
public bool isFiring;
public BulletController bullet;
public float bulletSpeed;
public float fireRate;
private float shotCounter;
public Transform bulletOrigin;
void Update()
{
/// Instantiate bullets
if (isFiring)
{
shotCounter -= Time.deltaTime;
if (shotCounter <= 0)
{
shotCounter = fireRate;
BulletController newBullet = Instantiate(bullet, bulletOrigin.position, bulletOrigin.rotation) as BulletController;
newBullet.speed = bulletSpeed;
}
}
else
{
shotCounter = 0;
}
///
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment