-
Notifications
You must be signed in to change notification settings - Fork 0
/
FishingMiniGame.cs
123 lines (99 loc) · 3.09 KB
/
FishingMiniGame.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FishingMiniGame : MonoBehaviour
{
[SerializeField] Transform TopPivot;
[SerializeField] Transform BottomPivot;
[SerializeField] Transform fish;
float fishPosition;
float fishDestination;
float fishTimer;
[SerializeField] float timerMultiplicator = 3f;
float fishSpeed;
[SerializeField] float smoothMotion = 1f;
[SerializeField] Transform hook;
float hookPosition;
[SerializeField]float hookSize= 0.1f;
[SerializeField]float hookPower= 0.5f;
float hookProgress;
float hookPullVelocity;
[SerializeField] float hookPullPower = 0.01f;
[SerializeField] float hookGravityPower = 0.005f;
[SerializeField] float hookProgressDegradationPower = 0.1f;
[SerializeField] SpriteRenderer hookSpriteRenderer ;
[SerializeField] Transform progressBarContainer;
bool pause = false;
[SerializeField] float failTimer = 10f;
private void Start(){
Resize();
}
private void Resize(){
Bounds b = hookSpriteRenderer.bounds;
float ySize=b.size.y;
Vector3 ls = hook.localScale;
float distance = Vector3.Distance(TopPivot.position,BottomPivot.position);
ls.y = (distance / ySize * hookSize);
hook.localScale = ls;
}
private void update()
{
if (pause){return;}
Fish();
Hook();
ProgressCheck();
}
private void ProgressCheck(){
Vector3 ls = progressBarContainer.localScale;
ls.y = hookProgress;
progressBarContainer.localScale = ls;
float min = hookPosition - hookSize /2 ;
float max = hookPosition + hookSize /2 ;
if( min < fishPosition && fishPosition < max)
{
hookProgress += hookPower * Time.deltaTime;
}
else{
hookProgress -= hookProgressDegradationPower * Time.deltaTime;
failTimer -= Time.deltaTime;
if(failTimer < 0f)
{
Lose();
}
}
if(hookProgress >= 1f)
{
Win();
}
hookProgress= Mathf.Clamp(hookProgress,0f ,1f);
}
private void Lose(){
pause = true;
Debug.Log ("YOU LOSE! FISH SWIM AWAY FROM YOU!");
}
private void Win(){
pause = true;
Debug.Log ("YOU WIN CATCH THE FISH!");
}
void Hook(){
if(Input.GetMouseButton(0))
{
hookPullVelocity += hookPullPower * Time.deltaTime;
}
hookPullVelocity -= hookGravityPower;
hookPosition += hookPullVelocity;
hookPosition = Mathf.Clamp(hookPosition,hookSize /2,1 - hookSize /2);
hook.position = Vector3.Lerp(BottomPivot.position,TopPivot.position,hookPosition);
}
void Fish ()
{
fishTimer -= Time.deltaTime;
if(fishTimer < 0f)
{
fishTimer = UnityEngine.Random.value * timerMultiplicator;
fishDestination = UnityEngine.Random.value;
}
fishPosition = Mathf.SmoothDamp(fishPosition,fishDestination, ref fishSpeed, smoothMotion);
fish.position = Vector3.Lerp(BottomPivot.position,TopPivot.position,fishPosition);
}
}