68 lines
1.8 KiB
C#
68 lines
1.8 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.GetButtonDown("Fire1"))
|
|
{
|
|
Prefab = Random.Range(0, 1);
|
|
Instantiate(Prefabs[Prefab], FirePoint.transform.position, FirePoint.transform.rotation);
|
|
Shoot();
|
|
}*/
|
|
//if(Input.GetButtonDown("")
|
|
if (Input.GetMouseButton(0) && fireCountdown <= 0f)
|
|
{
|
|
Instantiate(Prefabs[Prefab], FirePoint.transform.position, FirePoint.transform.rotation);
|
|
fireCountdown = 0;
|
|
fireCountdown += hSliderValue;
|
|
Shoot();
|
|
}
|
|
fireCountdown -= Time.deltaTime;
|
|
}
|
|
|
|
void Shoot()
|
|
{
|
|
RaycastHit hit;
|
|
if (Physics.Raycast(raycast.transform.position, raycast.transform.forward, out hit, range))
|
|
{
|
|
Debug.Log(hit.transform.name);
|
|
|
|
Target target = hit.transform.GetComponent<Target>();
|
|
|
|
if (target != null)
|
|
{
|
|
target.TakeDamage(damage);
|
|
}
|
|
RotateToGunDirection(gun, hit.point);
|
|
}
|
|
}
|
|
|
|
void RotateToGunDirection(GameObject obj, Vector3 destination)
|
|
{
|
|
direction = destination - obj.transform.position;
|
|
rotation = Quaternion.LookRotation(direction);
|
|
obj.transform.localRotation = Quaternion.Lerp(obj.transform.rotation, rotation, 1);
|
|
}
|
|
}
|