-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoneThrowing.cs
More file actions
115 lines (94 loc) · 2.8 KB
/
BoneThrowing.cs
File metadata and controls
115 lines (94 loc) · 2.8 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
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
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class BoneThrowing : MonoBehaviour {
[SerializeField] private Text levelIndication;
[SerializeField] private Text SensitivityDisplay;
[SerializeField] private Slider SensitivitySlider;
public GameObject dog;
public GameObject _camera;
public Canvas restartSreen;
private Dog dogScript;
private bool grounded;
private bool rotate;
public bool ReadyToThrow;
public bool throwing;
public bool pastLevel;
public bool GotCatch;
public float groundColliderOffsetX;
public float groundColliderOffsetY;
public float rotation = 10f;
public float MinTimeDelta = 0.1f;
public float MaxTimeDelta = 0.5f;
private float jumpForcex;
private float jumpForcey;
private float TimeStartThrow;
private float mouseXi,mouseYi;
private float sensitivity = 0.005f;
private Rigidbody2D bone;
void Start(){
bone = GetComponent<Rigidbody2D> ();
dogScript = dog.GetComponent<Dog> ();
throwing = false;
}
void OnCollisionStay2D(Collision2D hit)
{
grounded = true;
}
void OnCollisionEnter2D(Collision2D hit)
{
rotate = false;
}
void OnCollisionExit2D(Collision2D hit)
{
grounded = false;
}
void OnMouseDown(){
if (Input.GetMouseButtonDown (0) && ReadyToThrow) {
mouseXi = Input.mousePosition.x;
mouseYi = Input.mousePosition.y;
TimeStartThrow = Time.time;
throwing = true;
}
}
// Update is called once per frame
void Update () {
if (rotate) {
if(bone.velocity.x < 0)
this.transform.Rotate (new Vector3 (0, 0, rotation));
else
this.transform.Rotate (new Vector3 (0, 0, -rotation));
}
if (Input.GetMouseButtonUp (0) && ReadyToThrow && throwing) {
if (grounded == true && Mathf.Abs (bone.velocity.x) < 0.01) {
float timeDelta = Mathf.Lerp (MinTimeDelta, MaxTimeDelta, Time.time - TimeStartThrow);
jumpForcex = (Input.mousePosition.x - mouseXi) * sensitivity / timeDelta;
jumpForcey = (Input.mousePosition.y - mouseYi) * sensitivity / timeDelta;
throwing = false;
}
}
//Bone Missed
if (grounded == true && Mathf.Abs(bone.velocity.x) < 0.01 && !GotCatch && !restartSreen.enabled) {
dogScript.MissThrowFace.SetActive(true);
GotCatch = true; //just reuse the existing bool to prevent invoking the screen.
Invoke("PopScreen",1f);
}
//update sensitivity display and value
sensitivity = SensitivitySlider.value;
SensitivityDisplay.text = "Sensitivity: " + sensitivity;
}
void PopScreen(){
restartSreen.enabled = true;
}
void FixedUpdate(){
if (jumpForcex > 0 || jumpForcey > 0 && ReadyToThrow) {
bone.AddForce (new Vector2 (jumpForcex, jumpForcey), ForceMode2D.Impulse);
jumpForcex = jumpForcey = 0;
rotate = true;
ReadyToThrow = false;
GotCatch = false;
}
if (transform.position.y > levelIndication.transform.position.y)
pastLevel = true;
}
}