summaryrefslogtreecommitdiff
path: root/ch.sycoforge.Decal.Demo/AdvancedBulletHoles.cs
blob: 28e5311dbe4716cdc9c03dd4eec766b6e9b34604 (plain)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using UnityEngine;

namespace ch.sycoforge.Decal.Demo;

public class AdvancedBulletHoles : MonoBehaviour
{
	public EasyDecal DecalPrefab;

	public GameObject ImpactParticles;

	public float CastRadius = 0.25f;

	private void Start()
	{
		if (DecalPrefab == null)
		{
			Debug.LogError("The AdvancedBulletHoles script has no decal prefab attached.");
		}
		EasyDecal.HideMesh = false;
	}

	private void Update()
	{
		if (!Input.GetMouseButtonUp(0))
		{
			return;
		}
		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		if (!Physics.Raycast(ray, out var hitInfo, 200f))
		{
			return;
		}
		GameObject receiver = hitInfo.collider.gameObject;
		Vector3 point = hitInfo.point;
		RaycastHit[] array = Physics.SphereCastAll(ray, CastRadius, Vector3.Distance(Camera.main.transform.position, point) + 2f);
		Vector3 normal = hitInfo.normal;
		if (array.Length > 0)
		{
			RaycastHit[] array2 = array;
			for (int i = 0; i < array2.Length; i++)
			{
				RaycastHit raycastHit = array2[i];
				Debug.DrawLine(ray.origin, raycastHit.point, Color.red);
				normal += raycastHit.normal;
			}
		}
		normal /= (float)(array.Length + 1);
		EasyDecal.ProjectAt(DecalPrefab.gameObject, receiver, point, normal);
		if (ImpactParticles != null)
		{
			Quaternion rotation = Quaternion.FromToRotation(Vector3.up, normal);
			Object.Instantiate(ImpactParticles, point, rotation);
		}
	}
}