From 6ce8b9e22fc13be34b442c7b6af48b42cd44275a Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Wed, 13 Mar 2024 11:00:58 +0800 Subject: +init --- ch.sycoforge.Decal.Demo/AdvancedBulletHoles.cs | 55 ++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 ch.sycoforge.Decal.Demo/AdvancedBulletHoles.cs (limited to 'ch.sycoforge.Decal.Demo/AdvancedBulletHoles.cs') diff --git a/ch.sycoforge.Decal.Demo/AdvancedBulletHoles.cs b/ch.sycoforge.Decal.Demo/AdvancedBulletHoles.cs new file mode 100644 index 0000000..28e5311 --- /dev/null +++ b/ch.sycoforge.Decal.Demo/AdvancedBulletHoles.cs @@ -0,0 +1,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); + } + } +} -- cgit v1.1-26-g67d0