Skip to content

Commit 618c21c

Browse files
committed
finished player movement and animation
1 parent e8b78ba commit 618c21c

File tree

2 files changed

+105
-14
lines changed

2 files changed

+105
-14
lines changed

Assets/Scenes/MainGame.unity

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,9 @@ MonoBehaviour:
202202
m_Name:
203203
m_EditorClassIdentifier:
204204
movementAction: {fileID: -3629586159280527992, guid: 3657f75476d928345969072723e73df1, type: 3}
205-
speed: 1
205+
speed: 9
206+
turnSpeed: 20
207+
smoothTime: 0.05
206208
--- !u!114 &322310125
207209
MonoBehaviour:
208210
m_ObjectHideFlags: 0
@@ -540,7 +542,7 @@ MonoBehaviour:
540542
m_PanelSettings: {fileID: 11400000, guid: cd9dd175fa8971540a23834576355ef8, type: 2}
541543
m_ParentUI: {fileID: 0}
542544
sourceAsset: {fileID: 9197481963319205126, guid: 53831bd21265c7048a57773ecd74580c, type: 3}
543-
m_SortingOrder: 0
545+
m_SortingOrder: 1
544546
--- !u!1 &1638134665
545547
GameObject:
546548
m_ObjectHideFlags: 0

Assets/Scripts/Player/PlayerController.cs

Lines changed: 101 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,119 @@ public class PlayerController : MonoBehaviour {
99
[SerializeField]
1010
private float speed;
1111

12+
[SerializeField]
13+
private float turnSpeed;
14+
15+
[SerializeField]
16+
private float smoothTime;
17+
18+
19+
private Rigidbody playerRigidbody;
20+
21+
private Vector2 currentSmoothDampVelocity;
22+
23+
24+
private Vector3 initialPosition;
25+
private Quaternion initialRotation;
26+
27+
28+
private void Start() {
29+
playerRigidbody = GetComponent<Rigidbody>();
30+
31+
initialPosition = transform.position;
32+
initialRotation = transform.rotation;
33+
34+
GameManager.Instance.onGameStateChanged += GameStateChanged;
35+
}
36+
37+
private void OnDestroy() {
38+
if (GameManager.Instance != null) {
39+
GameManager.Instance.onGameStateChanged -= GameStateChanged;
40+
}
41+
}
1242

1343
private void Update() {
14-
var newPosition = transform.position + (Vector3) (speed * Time.deltaTime * movementAction.action.ReadValue<Vector2>());
44+
if (GameManager.Instance.GameState == GameState.Running) {
45+
CalculateNewVelocity();
46+
47+
CheckBoundaries();
1548

16-
var lowerBoundary = GameManager.Instance.GameLowerBoundaries;
17-
var upperBoundary = GameManager.Instance.GameUpperBoundaries;
49+
RotateByVelocity();
50+
}
51+
}
1852

19-
if (newPosition.x < lowerBoundary.x) {
20-
newPosition.x = lowerBoundary.x;
2153

22-
} else if (newPosition.x > upperBoundary.x) {
23-
newPosition.x = upperBoundary.x;
54+
private void CalculateNewVelocity() {
55+
var newVelocity = speed * movementAction.action.ReadValue<Vector2>();
56+
57+
newVelocity = Vector2.SmoothDamp(
58+
playerRigidbody.velocity,
59+
newVelocity,
60+
ref currentSmoothDampVelocity,
61+
smoothTime
62+
);
63+
64+
playerRigidbody.velocity = newVelocity;
65+
}
66+
67+
private void CheckBoundaries() {
68+
var lowerBoundaries = GameManager.Instance.GameLowerBoundaries;
69+
var upperBoundaries = GameManager.Instance.GameUpperBoundaries;
70+
71+
var position = transform.position;
72+
var velocity = playerRigidbody.velocity;
73+
74+
if (position.x < lowerBoundaries.x) {
75+
position.x = lowerBoundaries.x;
76+
velocity.x = 0;
77+
78+
} else if (position.x > upperBoundaries.x) {
79+
position.x = upperBoundaries.x;
80+
velocity.x = 0;
2481
}
2582

26-
if (newPosition.y < lowerBoundary.y) {
27-
newPosition.y = lowerBoundary.y;
83+
if (position.y < lowerBoundaries.y) {
84+
position.y = lowerBoundaries.y;
85+
velocity.y = 0;
2886

29-
} else if (newPosition.y > upperBoundary.y) {
30-
newPosition.y = upperBoundary.y;
87+
} else if (position.y > upperBoundaries.y) {
88+
position.y = upperBoundaries.y;
89+
velocity.y = 0;
3190
}
3291

33-
transform.position = newPosition;
92+
transform.position = position;
93+
playerRigidbody.velocity = velocity;
94+
}
95+
96+
private void RotateByVelocity() {
97+
var direction = Quaternion.LookRotation(
98+
playerRigidbody.velocity + (Vector3.forward * speed)
99+
);
100+
101+
var rotation = Quaternion.Slerp(
102+
transform.rotation,
103+
direction,
104+
turnSpeed * Time.deltaTime
105+
);
106+
107+
playerRigidbody.MoveRotation(
108+
rotation
109+
);
34110
}
35111

112+
113+
private void GameStateChanged(GameState newState) {
114+
if (newState == GameState.Stopped) {
115+
playerRigidbody.velocity = Vector3.zero;
116+
117+
transform.SetPositionAndRotation(
118+
initialPosition,
119+
initialRotation
120+
);
121+
}
122+
}
123+
124+
36125
private void OnTriggerEnter(Collider other) {
37126
if (other.gameObject.TryGetComponent<PointController>(out var pointController)) {
38127
GameManager.Instance.CurrentScore += pointController.Value;

0 commit comments

Comments
 (0)