79 lines
2.2 KiB
C#
79 lines
2.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class HS_RaycastInstance : MonoBehaviour
|
|
{
|
|
public Camera Cam;
|
|
public GameObject[] Prefabs;
|
|
private int Prefab;
|
|
private Ray RayMouse;
|
|
private GameObject Instance;
|
|
private float windowDpi;
|
|
|
|
//Double-click protection
|
|
private float buttonSaver = 0f;
|
|
|
|
void Start()
|
|
{
|
|
if (Screen.dpi < 1) windowDpi = 1;
|
|
if (Screen.dpi < 200) windowDpi = 1;
|
|
else windowDpi = Screen.dpi / 200f;
|
|
Counter(0);
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (Input.GetButtonDown("Fire1"))
|
|
{
|
|
if (Cam != null)
|
|
{
|
|
RaycastHit hit;
|
|
var mousePos = Input.mousePosition;
|
|
RayMouse = Cam.ScreenPointToRay(mousePos);
|
|
if (Physics.Raycast(RayMouse.origin, RayMouse.direction, out hit, 40))
|
|
{
|
|
Instance = Instantiate(Prefabs[Prefab]);
|
|
Instance.transform.position = hit.point + hit.normal * 0.01f;
|
|
Destroy(Instance, 1.5f);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("No camera");
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
void OnGUI()
|
|
{
|
|
GUI.Label(new Rect(10 * windowDpi, 5 * windowDpi, 400 * windowDpi, 20 * windowDpi), "Use the keyboard buttons A/<- and D/-> to change prefabs!");
|
|
GUI.Label(new Rect(10 * windowDpi, 20 * windowDpi, 400 * windowDpi, 20 * windowDpi), "Use left mouse button for instancing!");
|
|
}
|
|
|
|
void Counter(int count)
|
|
{
|
|
Prefab += count;
|
|
if (Prefab > Prefabs.Length - 1)
|
|
{
|
|
Prefab = 0;
|
|
}
|
|
else if (Prefab < 0)
|
|
{
|
|
Prefab = Prefabs.Length - 1;
|
|
}
|
|
}
|
|
}
|