-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerMove.cs
More file actions
93 lines (65 loc) · 2 KB
/
PlayerMove.cs
File metadata and controls
93 lines (65 loc) · 2 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
/*======================================================================
* Player Move
*
* Programmer: David Torrente
*
* Description: This class moves the player front, back, left, and
* right. It also includes the jump movement. It does not rotate
* the player. This code will go on the actual player object.
* ====================================================================*/
using UnityEngine;
using System.Collections;
public class PlayerMove : MonoBehaviour {
public float speed = 5;
public int playerNum;
public float jumpSpeed = 17.0F;
public float maxJump = 9.0F;
private bool jumpState;
private bool canJump;
private float groundedYPos;
private CharacterController cController;
void Start () {
cController = GetComponent<CharacterController> ();
canJump = true;
jumpState = false;
groundedYPos = transform.position.y;
}
void Update () {
Vector3 moveDir = new Vector3(Input.GetAxis("Horizontal" + playerNum), jump(), Input.GetAxis("Vertical" + playerNum));
moveDir = transform.TransformDirection(moveDir);
cController.Move(moveDir*speed*Time.deltaTime);
}
float jump() {
float deltaY = -1;
if (jumpState)
{
if (Input.GetButton("Jump" + playerNum) && (transform.position.y < (groundedYPos+maxJump) && cController.velocity.y > 0))
deltaY = 1;
else
{
jumpState = false;
deltaY = -1;
}
if(cController.isGrounded)
{
jumpState = false;
deltaY = 0;
groundedYPos = transform.position.y;
}
}
if (cController.isGrounded && !(Input.GetButton("Jump" + playerNum)))
{
canJump = true;
groundedYPos = transform.position.y;
}
//clean up at the end in order to reset the jump state.
if (cController.isGrounded && Input.GetButton("Jump" + playerNum) && canJump)
{
jumpState = true;
canJump = false;
groundedYPos = transform.position.y;
deltaY = 1;
}
return deltaY;
}
}