Files
real-shooter/Assets/Scripts/Weapons/Shooting.cs
2022-05-15 18:24:43 +07:00

48 lines
1.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shooting : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public GameObject raycast;
public GameObject FirePoint;
public GameObject[] Prefabs;
private float hSliderValue = 0.1f;
private float fireCountdown = 0f;
private int Prefab = 1;
private Ray RayMouse;
private Vector3 direction;
private Quaternion rotation;
public GameObject gun;
void Update()
{
if (Input.GetMouseButton(0) && fireCountdown <= 0f)
{
Instantiate(Prefabs[Prefab], FirePoint.transform.position, FirePoint.transform.rotation);
fireCountdown = 0;
fireCountdown += hSliderValue;
Shoot();
}
fireCountdown -= Time.deltaTime;
}
public void Shoot()
{
if (Physics.Raycast(raycast.transform.position, raycast.transform.forward, out var hit, range))
{
Debug.Log(hit.transform.name);
if (hit.transform.TryGetComponent<Target>(out var target))
target.TakeDamage(damage);
}
}
}