-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerController.cs
83 lines (69 loc) · 2.14 KB
/
PlayerController.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
79
80
81
82
83
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
private float playerSpeed = 5f;
private Animator m_animator;
// Start is called before the first frame update
void Start() {
m_animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update() {
MovePlayer();
}
private void MovePlayer() {
Vector2 moveVector = GetMovementInput();
UpdatePlayerAnimator(moveVector.x, moveVector.y);
MovePlayerFree(moveVector);
}
private Vector2 GetMovementInput() {
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
// Lock directions to avoid diagonal movements
if (horizontal != 0f) {
vertical = 0f;
}
return new Vector2(horizontal, vertical);
}
// Free movement of player
private void MovePlayerFree(Vector2 moveVector) {
Vector3 position = transform.position;
if (moveVector.y > 0) {
position += Vector3.up;
}
if (moveVector.y < 0) {
position += Vector3.down;
}
if (moveVector.x > 0) {
position += Vector3.right;
}
if (moveVector.x < 0) {
position += Vector3.left;
}
transform.position = Vector3.MoveTowards(transform.position, position, Time.deltaTime*playerSpeed);
}
private void UpdatePlayerAnimator(float horizontal, float vertical) {
if (horizontal != 0f || vertical != 0f) {
m_animator.SetBool("Walking", true);
if (horizontal > 0f) {
m_animator.SetFloat("LastMoveHorizontal", 1f);
} else if (horizontal < 0f) {
m_animator.SetFloat("LastMoveHorizontal", -1f);
} else {
m_animator.SetFloat("LastMoveHorizontal", 0f);
}
if (vertical > 0f) {
m_animator.SetFloat("LastMoveVertical", 1f);
} else if (vertical < 0f) {
m_animator.SetFloat("LastMoveVertical", -1f);
} else {
m_animator.SetFloat("LastMoveVertical", 0f);
}
} else {
m_animator.SetBool("Walking", false);
}
m_animator.SetFloat("Horizontal", horizontal);
m_animator.SetFloat("Vertical", vertical);
}
}