119 lines
3.6 KiB
C#
119 lines
3.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.Serialization.Formatters;
|
|
using System;
|
|
using UnityEngine;
|
|
|
|
public class DemoShooting : MonoBehaviour
|
|
{
|
|
public GameObject FirePoint;
|
|
public Camera Cam;
|
|
public float MaxLength;
|
|
public GameObject[] Prefabs;
|
|
|
|
private Ray RayMouse;
|
|
private Vector3 direction;
|
|
private Quaternion rotation;
|
|
|
|
[Header("GUI")]
|
|
private float windowDpi;
|
|
private int Prefab;
|
|
private GameObject Instance;
|
|
private float hSliderValue = 0.1f;
|
|
private float fireCountdown = 0f;
|
|
|
|
//Double-click protection
|
|
private float buttonSaver = 0f;
|
|
|
|
//For Camera shake
|
|
public Animation camAnim;
|
|
|
|
void Start()
|
|
{
|
|
if (Screen.dpi < 1) windowDpi = 1;
|
|
if (Screen.dpi < 200) windowDpi = 1;
|
|
else windowDpi = Screen.dpi / 200f;
|
|
Counter(0);
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
//Single shoot
|
|
if (Input.GetMouseButtonDown(0))
|
|
{
|
|
camAnim.Play(camAnim.clip.name);
|
|
Instantiate(Prefabs[Prefab], FirePoint.transform.position, FirePoint.transform.rotation);
|
|
}
|
|
|
|
//Fast shooting
|
|
if (Input.GetMouseButton(1) && fireCountdown <= 0f)
|
|
{
|
|
Instantiate(Prefabs[Prefab], FirePoint.transform.position, FirePoint.transform.rotation);
|
|
fireCountdown = 0;
|
|
fireCountdown += hSliderValue;
|
|
}
|
|
fireCountdown -= Time.deltaTime;
|
|
|
|
//To change projectiles
|
|
if ((Input.GetKey(KeyCode.A) || Input.GetAxis("Horizontal") < 0) && buttonSaver >= 0.4f)// left button
|
|
{
|
|
buttonSaver = 0f;
|
|
Counter(-1);
|
|
}
|
|
if ((Input.GetKey(KeyCode.D) || Input.GetAxis("Horizontal") > 0) && buttonSaver >= 0.4f)// right button
|
|
{
|
|
buttonSaver = 0f;
|
|
Counter(+1);
|
|
}
|
|
buttonSaver += Time.deltaTime;
|
|
|
|
//To rotate fire point
|
|
if (Cam != null)
|
|
{
|
|
RaycastHit hit;
|
|
var mousePos = Input.mousePosition;
|
|
RayMouse = Cam.ScreenPointToRay(mousePos);
|
|
if (Physics.Raycast(RayMouse.origin, RayMouse.direction, out hit, MaxLength))
|
|
{
|
|
RotateToMouseDirection(gameObject, hit.point);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("No camera");
|
|
}
|
|
}
|
|
|
|
//GUI Text
|
|
void OnGUI()
|
|
{
|
|
GUI.Label(new Rect(10 * windowDpi, 5 * windowDpi, 400 * windowDpi, 20 * windowDpi), "Use left mouse button to single shoot!");
|
|
GUI.Label(new Rect(10 * windowDpi, 25 * windowDpi, 400 * windowDpi, 20 * windowDpi), "Use and hold the right mouse button for quick shooting!");
|
|
GUI.Label(new Rect(10 * windowDpi, 45 * windowDpi, 400 * windowDpi, 20 * windowDpi), "Fire rate:");
|
|
hSliderValue = GUI.HorizontalSlider(new Rect(70 * windowDpi, 50 * windowDpi, 100 * windowDpi, 20 * windowDpi), hSliderValue, 0.0f, 1.0f);
|
|
GUI.Label(new Rect(10 * windowDpi, 65 * windowDpi, 400 * windowDpi, 20 * windowDpi), "Use the keyboard buttons A/<- and D/-> to change projectiles!");
|
|
}
|
|
|
|
// To change prefabs (count - prefab number)
|
|
void Counter(int count)
|
|
{
|
|
Prefab += count;
|
|
if (Prefab > Prefabs.Length - 1)
|
|
{
|
|
Prefab = 0;
|
|
}
|
|
else if (Prefab < 0)
|
|
{
|
|
Prefab = Prefabs.Length - 1;
|
|
}
|
|
}
|
|
|
|
//To rotate fire point
|
|
void RotateToMouseDirection(GameObject obj, Vector3 destination)
|
|
{
|
|
direction = destination - obj.transform.position;
|
|
rotation = Quaternion.LookRotation(direction);
|
|
obj.transform.localRotation = Quaternion.Lerp(obj.transform.rotation, rotation, 1);
|
|
}
|
|
}
|