-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerMovement.cs
More file actions
61 lines (46 loc) · 1.33 KB
/
PlayerMovement.cs
File metadata and controls
61 lines (46 loc) · 1.33 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : 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();
}
// move player
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);
}
// apply gravity
void ApplyGravity()
{
vertical_Velocity -= gravity * Time.deltaTime;
// jump
PlayerJump();
move_Direction.y = vertical_Velocity * Time.deltaTime;
}
void PlayerJump()
{
if (character_Controller.isGrounded && Input.GetKeyDown(KeyCode.Space))
{
vertical_Velocity = jump_Force;
}
}
} // class