Created
November 14, 2021 15:23
-
-
Save didacus/01f50a7f763f525f3040f2bf10a208ae to your computer and use it in GitHub Desktop.
Unity - Basic gun controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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