-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJumpingMovementController.boo
More file actions
69 lines (54 loc) · 2.48 KB
/
Copy pathJumpingMovementController.boo
File metadata and controls
69 lines (54 loc) · 2.48 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
import UnityEngine
class JumpingMovementController(MonoBehaviour):
"""
This is exactly the same as the BaiscMovementController, except that it adds jumping.
Jumping requires the controller to keep track of something besides user inputs. We keep a
variable called '_Momentum' and when the user hits the jump button we add to the _Momentum.
Then we use that the same way we used the direct key inputs to move the controller.
To simulate gravity, we subtract from _Momentum at the end of every frame in the LateUpdate()
method. That slows the jump and then reverse it. To keep the controller from falling
forever, we check the position of the object each turn and if it is below 0 height, we
zero out the momentum and snap the position to exactly 0.
Note that this is pure fakery - it does not take account of the actual ground or objects
in the scene. But it's a good illustration of how to do it in a simple 2-D world
Style notes
-----------
- the variable _Momentum is not marked public. Public variables (like
_Speed) show up in the Inspector. Since _Momentum is managed by the controller
itself it does not need to be public (although sometimes it's a good idea to
make public variables so you can see what's going on while testing)
- _Gravity is also not public. It's always smart to put constants like this
into variables instead of hard-coding them into your scripts -- it makes it
far easier to read the code and also to experiment with changes.
"""
_HORIZ = 'Horizontal'
_VERT = 'Vertical'
_JUMP = 'Jump'
public _Speed = 1.0
public _JumpSpeed = 1.5
_Momentum = 0.0
_Gravity = 2
def Update():
"""
Moves on the 2-d plane like BaiscMovementController, but if users presses the jump
input we add upward momentum
"""
frame_speed = _Speed * Time.deltaTime
# only allow jumps if we are on the ground!
if transform.position.y == 0 :
_Momentum += Input.GetAxis(_JUMP) * _JumpSpeed
up = _Momentum * Time.deltaTime
left_right = Input.GetAxis(_HORIZ) * frame_speed
forward_back = Input.GetAxis(_VERT) * frame_speed
transform.Translate(Vector3(left_right, up, forward_back), Space.Self)
def LateUpdate():
"""
If the unit is in the air, deduct some momentum. If it is below the ground,
zero out both momentum and the height so we don't fall through the floor
"""
if transform.position.y > 0:
_Momentum -= _Gravity * Time.deltaTime;
else:
_Momentum = 0;
vp = Vector3(transform.position.x, 0, transform.position.z)
transform.position = vp