Skip to content

Commit 2fb47b2

Browse files
committed
Fixed so that player grounded is unaffected by gravity from (other) planets, partially fixes #5 and the commit Måns made a few commits back
1 parent d06f018 commit 2fb47b2

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

Assets/Scripts/GravitySink.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,29 @@
33

44
[RequireComponent(typeof(Rigidbody))]
55
public class GravitySink : MonoBehaviour {
6-
7-
bool autoApply = true;
86
Rigidbody body;
97
Vector3 force;
108
Vector3 prevForce;
119

10+
bool acceptsForce = true;
11+
bool autoApply = true;
12+
1213
// Use this for initialization
1314
void Start () {
1415
body = GetComponent<Rigidbody> ();
1516
}
1617

1718
public void addForce(Vector3 force) {
18-
this.force += force;
19+
// TODO: There is no need to even calculate the force if it is never applied
20+
// So the logic that calculates gravity should probably take the enabled
21+
// state of the GravitySink into consideration.
22+
if (acceptsForce) {
23+
this.force += force;
24+
}
25+
}
26+
27+
public void setAcceptsForce(bool acceptsForce) {
28+
this.acceptsForce = acceptsForce;
1929
}
2030

2131
public void setAutoApply(bool autoApply) {
@@ -33,7 +43,7 @@ void FixedUpdate() {
3343
void FixedUpdate() {
3444
if (autoApply) {
3545
body.AddForce (force);
46+
force = Vector3.zero;
3647
}
37-
force = Vector3.zero;
3848
}
3949
}

Assets/Scripts/PlayerController.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,16 @@ void movePlayer(float forwardCharacterInput, float sidewaysCharacterInput, float
227227
camerat = (camerat * Mathf.Cos (degree_rotation * Mathf.PI / 180) + trajectory2 * Mathf.Sin (degree_rotation * Mathf.PI / 180)).normalized;
228228
}
229229

230+
GravitySink gravitySink = this.GetComponent<GravitySink> ();
231+
230232
if (isBound || stuck) {
231233
// Måns is love, Måns is life
232234
// We should only do this when grounded, otherwise jumping will be weird
233235
// Also, we should inherit the planet velocity when grounded and jumping
234236

237+
// Don't apply gravity when grounded
238+
gravitySink.setAcceptsForce (false);
239+
235240
// Calculate how much the planet has moved since last cycle, and move the player along with it.
236241
Vector3 dv = planet.transform.position - planetProperties.position;
237242
planetProperties = new PlanetProperties (planet, playerHeight);
@@ -258,13 +263,14 @@ void movePlayer(float forwardCharacterInput, float sidewaysCharacterInput, float
258263
} else {
259264
body.velocity = factor * moveSpeed * (forwardOrBackwardVector + sidewaysVector).normalized;
260265
}
261-
if (isBound) {
262-
land ();
263-
}
266+
land ();
267+
264268
if (jump) {
265269
body.position += up * 2 * planetProperties.boundaryCondition;
266270
body.velocity += up * jumpSpeed;
267271
}
272+
} else {
273+
gravitySink.setAcceptsForce (true);
268274
}
269275

270276

0 commit comments

Comments
 (0)