-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeapon.cs
More file actions
41 lines (33 loc) · 981 Bytes
/
Weapon.cs
File metadata and controls
41 lines (33 loc) · 981 Bytes
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
41
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviour
{
public float weaponDamage = 10f;
private float weaponRange = 50f;
public float fireRate = 20;
public float nextFire = 0f;
public Camera fpsCamera;
public ParticleSystem gunFlash;
private void Update()
{
if (Input.GetButton("Fire1") && Time.time >= nextFire)
{
nextFire = Time.time + 1f / fireRate;
Shoot();
}
}
void Shoot()
{
gunFlash.Play();
RaycastHit hit;
if (Physics.Raycast(fpsCamera.transform.position, fpsCamera.transform.forward, out hit, weaponRange))
{
Debug.Log(hit.transform.name);
if (hit.transform.gameObject.tag == "Enemy")
{
hit.transform.GetComponent<Enemy>().DamageEnemy(weaponDamage);
}
}
}
}