-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMovement_WASD.cs
78 lines (39 loc) · 1.39 KB
/
Movement_WASD.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement_WASD : MonoBehaviour
{
private CharacterController character_Controller;
private Vector3 move_Direction;
public float speed = 5f;
private float gravity = 20f;
public float Jump_Force = 10f;
private float vertical_Velocity;
void Awake(){
character_Controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
MoveThePlayer();
}
void MoveThePlayer(){
move_Direction = new Vector3(Input.GetAxis(Axis.HORIZONTAL), 0f,
Input.GetAxis(Axis.VERTICAL));
move_Direction = transform.TransformDirection(move_Direction);
move_Direction *= speed * Time.deltaTime;
ApplyGravity();
character_Controller.Move(move_Direction);
}//MOvePlayer
void ApplyGravity(){
vertical_Velocity -= gravity * Time.deltaTime;
//jump
PlayerJump();
move_Direction.y = vertical_Velocity * Time.deltaTime;
}//apply gravity
void PlayerJump(){
if(character_Controller.isGrounded && Input.GetKeyDown(KeyCode.Space)) {
vertical_Velocity = Jump_Force;
}
}
} //class