-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturretGun.cs
40 lines (33 loc) · 1.02 KB
/
turretGun.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations.Rigging;
public class turretGun : MonoBehaviour
{ public Target target;
public GameObject BulletPrefab;
public Transform MuzzleTransform;
public float BulletSpeed;
public float FireRate = 10;
private float lastFired = 10;
public AudioSource firesound ;
private void LateUpdate() {
if (target.health > 0f )
{
if (Time.time > lastFired){
if (Time.time > lastFired)
{
lastFired += FireRate;
ShootBullet();
} }
}
void ShootBullet()
{
firesound.Play();
//instantiate the bullet
var bullet = Instantiate(BulletPrefab, MuzzleTransform.position, MuzzleTransform.rotation);
// give it velocity
bullet.GetComponent<Rigidbody>().velocity = MuzzleTransform.forward * BulletSpeed;
}
}
}