Slicer.cs
1.66 KB
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
using UnityEngine;
using EzySlice;
public class Slicer : MonoBehaviour
{
public Material materialAfterSlice;
public LayerMask sliceMask;
public bool isTouched;
private void Update()
{
if (isTouched == true)
{
isTouched = false;
Collider[] objectsToBeSliced = Physics.OverlapBox(transform.position, new Vector3(1, 0.1f, 0.1f), transform.rotation, sliceMask);
foreach (Collider objectToBeSliced in objectsToBeSliced)
{
SlicedHull slicedObject = SliceObject(objectToBeSliced.gameObject, materialAfterSlice);
GameObject upperHullGameobject = slicedObject.CreateUpperHull(objectToBeSliced.gameObject, materialAfterSlice);
GameObject lowerHullGameobject = slicedObject.CreateLowerHull(objectToBeSliced.gameObject, materialAfterSlice);
upperHullGameobject.transform.position = objectToBeSliced.transform.position;
lowerHullGameobject.transform.position = objectToBeSliced.transform.position;
MakeItPhysical(upperHullGameobject);
MakeItPhysical(lowerHullGameobject);
Destroy(objectToBeSliced.gameObject);
}
}
}
private void MakeItPhysical(GameObject obj)
{
obj.AddComponent<MeshCollider>().convex = true;
obj.AddComponent<Rigidbody>();
obj.layer = LayerMask.NameToLayer("Sliceable");
obj.AddComponent<OVRGrabbable>();
}
private SlicedHull SliceObject(GameObject obj, Material crossSectionMaterial = null)
{
return obj.Slice(transform.position, transform.up, crossSectionMaterial);
}
}