-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDrawLineScript.cs
126 lines (80 loc) · 3.25 KB
/
DrawLineScript.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class DrawLineLoveBalls : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
GameObject LineGO;
bool StartDrawing;
Vector3 MousePos;
LineRenderer LR;
[SerializeField]
Material LineMat;
int CurrentIndex;
[SerializeField]
Camera cam;
[SerializeField]
Transform Collider_Prefab;
Transform LastInstantiated_Collider;
//for Love Balls
//[SerializeField]
//List<Rigidbody> RB = new List<Rigidbody>();
public void OnPointerDown(PointerEventData eventData)
{
StartDrawing = true;
MousePos = Input.mousePosition;
LR = LineGO.AddComponent<LineRenderer>();
LR.startWidth = 0.2f;
LR.material = LineMat;
}
public void OnPointerUp(PointerEventData eventData)
{
StartDrawing = false;
Rigidbody rb = LineGO.AddComponent<Rigidbody>();
rb.constraints = RigidbodyConstraints.FreezeRotationX;
LR.useWorldSpace = false;
Destroy(LastInstantiated_Collider.gameObject);
Start();
CurrentIndex = 0;
//for love balls
//foreach(Rigidbody SphereRB in RB )
//{
//SphereRB.useGravity = true;
//}
}
void Start()
{
LineGO = new GameObject();
}
void FixedUpdate()
{
if (StartDrawing)
{
Vector3 Dist = MousePos - Input.mousePosition;
float Distance_SqrMag = Dist.sqrMagnitude;
if (Distance_SqrMag > 1000f)
{
// Set this Position for our line
LR.SetPosition(CurrentIndex, cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z + 10f)));
if (LastInstantiated_Collider != null)
{
Vector3 CurLinePos = LR.GetPosition(CurrentIndex);
LastInstantiated_Collider.gameObject.SetActive(true);
LastInstantiated_Collider.LookAt(CurLinePos);
if (LastInstantiated_Collider.rotation.y == 0)
{
//Debug.Log(LastInstantiated_Collider);
LastInstantiated_Collider.eulerAngles = new Vector3(LastInstantiated_Collider.rotation.eulerAngles.x, 90, LastInstantiated_Collider.rotation.eulerAngles.z);
}
LastInstantiated_Collider.localScale = new Vector3(LastInstantiated_Collider.localScale.x, LastInstantiated_Collider.localScale.y, Vector3.Distance(LastInstantiated_Collider.position, CurLinePos) * 0.5f);
}
LastInstantiated_Collider = Instantiate(Collider_Prefab, LR.GetPosition(CurrentIndex), Quaternion.identity, LineGO.transform);
LastInstantiated_Collider.gameObject.SetActive(false);
MousePos = Input.mousePosition;
CurrentIndex++;
LR.positionCount = CurrentIndex + 1;
LR.SetPosition(CurrentIndex, cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z + 10f)));
}
}
}
}