-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActiveOutline.cs
81 lines (71 loc) · 3.27 KB
/
ActiveOutline.cs
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using UnityEngine;
namespace cakeslice
{
public class ActiveOutline : MonoBehaviour
{
[HideInInspector] public bool EnMain;
[HideInInspector] public Rigidbody RbObjet;
[HideInInspector] public GameObject objet;
[HideInInspector] public Color DebugRayColor = Color.red;
public GameObject centerHand;
public float Distance = 4.0F;
void Update()
{
//si main vide
if (!EnMain)
{
//créer un rayon de raycast
Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0F));
Debug.DrawRay(ray.origin, ray.direction * Distance, DebugRayColor);
RaycastHit hit;
//si le Hit est avéré et l'objet a le tag "pickable"
if (Physics.Raycast(ray, out hit, Distance) && hit.collider.tag == "Pickable")
{
//appliquer le outline
Outline ObjOut = hit.rigidbody.gameObject.GetComponent<Outline>();
ObjOut.eraseRenderer = true;
if (Input.GetMouseButtonUp(0))
{
//récupération d'nfos sur l'objet cliqué
objet = hit.transform.gameObject;
RbObjet = objet.GetComponent<Rigidbody>();
//placer le child de la main au centre de l'objet (pour faciliter la rotation)
//on appliquera une rotation sur cet axe et non sur l'objet lui même (trop complexe)
centerHand.transform.position = objet.transform.position;
//attrapper l'objet pour qu'il suive la main
objet.transform.SetParent(centerHand.transform);
//modifier les attribus de l'objet
RbObjet.useGravity = false;
RbObjet.isKinematic = true;
Debug.Log("<color=GREEN><b> Attrapé </b> </color>");
EnMain = true;
return; //quitter la boucle
}
}
}
else
{
//lacher l'objet
if (Input.GetMouseButtonUp(0))
{
Debug.Log("<color=yellow><b> Laché </b> </color>");
Rigidbody rb = transform.GetComponentInChildren<Rigidbody>();
rb.transform.parent = null;
//reset des attributs de l'objet
RbObjet.useGravity = true;
RbObjet.isKinematic = false;
EnMain = false;
}
//rotation du child de la main (et par conséquent de l'objet)
if (Input.GetAxis("Mouse ScrollWheel") > 0f) // forward
{
centerHand.transform.Rotate(new Vector3(1, 0, 0));
}
else if (Input.GetAxis("Mouse ScrollWheel") < 0f) // backwards
{
centerHand.transform.Rotate(new Vector3(-1, 0, 0));
}
}
}
}
}