-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode
More file actions
39 lines (32 loc) · 844 Bytes
/
Copy pathcode
File metadata and controls
39 lines (32 loc) · 844 Bytes
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
public Rigidbody2D bird;
public Transform slingshotPivot;
private Vector2 startPos;
private bool isDragging = false;
void Start()
{
startPos = bird.position;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
isDragging = true;
if (Input.GetMouseButtonUp(0) && isDragging)
{
isDragging = false;
LaunchBird();
}
if (isDragging)
DragBird();
}
void DragBird()
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePos.z = 0;
bird.position = mousePos;
}
void LaunchBird()
{
Vector2 launchVelocity = (startPos - bird.position) * 5;
bird.isKinematic = false;
bird.AddForce(launchVelocity, ForceMode2D.Impulse);
}