1
+ using System . Collections ;
2
+ using System . Collections . Generic ;
3
+ using UnityEngine ;
4
+
5
+ public class MyInput : MonoBehaviour {
6
+ //移动方向枚举
7
+ enum MoveDir
8
+ {
9
+ None = 0 , //不动
10
+ Up = 1 , //上8
11
+ Down = - 1 , //下2
12
+ Left = 10 , //左4
13
+ Right = - 10 , //右6
14
+ UL = 11 , //左上7
15
+ UR = - 9 , //右上9
16
+ DL = 9 , //左下1
17
+ DR = - 11 , //右下3
18
+ }
19
+
20
+ //输入按键常量(之后走配置)
21
+ const KeyCode INPUT_UP = KeyCode . W ;
22
+ const KeyCode INPUT_DOWN = KeyCode . S ;
23
+ const KeyCode INPUT_LEFT = KeyCode . A ;
24
+ const KeyCode INPUT_RIGHT = KeyCode . D ;
25
+
26
+ //默认移动方向
27
+ private MoveDir moveDir = MoveDir . None ;
28
+ //按压值
29
+ private int moveDirValue = 0 ;
30
+ //按压记录
31
+ private bool isUpPress = false ;
32
+ private bool isDownPress = false ;
33
+ private bool isLeftPress = false ;
34
+ private bool isRightPress = false ;
35
+
36
+ //是否可以移动
37
+ private bool canMove = true ;
38
+ //右移动
39
+ private Vector3 MOVE_RIGHT = new Vector3 ( 1 , 0 , 0 ) ;
40
+ //上移动
41
+ private Vector3 MOVE_UP = new Vector3 ( 0 , 1 , 0 ) ;
42
+
43
+ //外部调控速度
44
+ public float speed = 2f ;
45
+ //移动速度向量
46
+ private Vector3 move_speed_dir = Vector3 . zero ;
47
+ //移动距离
48
+ private Vector3 move_dis = Vector3 . zero ;
49
+
50
+ //控制目标
51
+ public Transform target ;
52
+
53
+ // Use this for initialization
54
+ void Start ( ) {
55
+
56
+ }
57
+
58
+ // Update is called once per frame
59
+ void Update ( ) {
60
+ CheckInputKey ( ) ;
61
+ CheckMoveDir ( ) ;
62
+ }
63
+
64
+ void FixedUpdate ( )
65
+ {
66
+ CheckMove ( ) ;
67
+ }
68
+
69
+ //检测输入按键
70
+ void CheckInputKey ( )
71
+ {
72
+ //检测单一输入
73
+ foreach ( KeyCode kcode in System . Enum . GetValues ( typeof ( KeyCode ) ) )
74
+ {
75
+ if ( Input . GetKeyDown ( kcode ) )
76
+ {
77
+ Debug . Log ( "Single KeyCode: " + kcode ) ;
78
+ ChangeKeyPressState ( kcode , true ) ;
79
+ }
80
+
81
+ if ( Input . GetKeyUp ( kcode ) )
82
+ {
83
+ Debug . Log ( "Single KeyCode: " + kcode ) ;
84
+ ChangeKeyPressState ( kcode , false ) ;
85
+ }
86
+ }
87
+ }
88
+
89
+ //记录按键的按压状态
90
+ void ChangeKeyPressState ( KeyCode keyCode , bool isPress )
91
+ {
92
+ switch ( keyCode )
93
+ {
94
+ case INPUT_UP :
95
+ isUpPress = isPress ;
96
+ break ;
97
+ case INPUT_DOWN :
98
+ isDownPress = isPress ;
99
+ break ;
100
+ case INPUT_LEFT :
101
+ isLeftPress = isPress ;
102
+ break ;
103
+ case INPUT_RIGHT :
104
+ isRightPress = isPress ;
105
+ break ;
106
+ }
107
+ }
108
+
109
+ //确定移动方向
110
+ void CheckMoveDir ( )
111
+ {
112
+ moveDirValue = 0 ;
113
+ //确定方向
114
+ if ( isUpPress )
115
+ {
116
+ moveDirValue += ( int ) MoveDir . Up ;
117
+ }
118
+ if ( isDownPress )
119
+ {
120
+ moveDirValue += ( int ) MoveDir . Down ;
121
+ }
122
+ if ( isLeftPress )
123
+ {
124
+ moveDirValue += ( int ) MoveDir . Left ;
125
+ }
126
+ if ( isRightPress )
127
+ {
128
+ moveDirValue += ( int ) MoveDir . Right ;
129
+ }
130
+ }
131
+
132
+ //检测是否可以移动
133
+ void CheckMove ( )
134
+ {
135
+ //某些情况下可能禁止移动,例如暂停,播放CG等
136
+ if ( canMove && moveDirValue != ( int ) MoveDir . None )
137
+ {
138
+ PlayerMove ( target , speed ) ;
139
+ }
140
+ }
141
+
142
+ //移动
143
+ void PlayerMove ( Transform target , float speed )
144
+ {
145
+ move_dis = speed * Time . deltaTime * GetSpeedDir ( ) ;
146
+ target . position += move_dis ;
147
+ }
148
+
149
+ //速度向量
150
+ Vector3 GetSpeedDir ( )
151
+ {
152
+ switch ( moveDirValue )
153
+ {
154
+ case ( int ) MoveDir . Up :
155
+ move_speed_dir = MOVE_UP ;
156
+ break ;
157
+ case ( int ) MoveDir . Down :
158
+ move_speed_dir = - MOVE_UP ;
159
+ break ;
160
+ case ( int ) MoveDir . Left :
161
+ move_speed_dir = - MOVE_RIGHT ;
162
+ break ;
163
+ case ( int ) MoveDir . Right :
164
+ move_speed_dir = MOVE_RIGHT ;
165
+ break ;
166
+ case ( int ) MoveDir . UL :
167
+ move_speed_dir = MOVE_UP - MOVE_RIGHT ;
168
+ break ;
169
+ case ( int ) MoveDir . UR :
170
+ move_speed_dir = MOVE_UP + MOVE_RIGHT ;
171
+ break ;
172
+ case ( int ) MoveDir . DL :
173
+ move_speed_dir = - MOVE_UP - MOVE_RIGHT ;
174
+ break ;
175
+ case ( int ) MoveDir . DR :
176
+ move_speed_dir = - MOVE_UP + MOVE_RIGHT ;
177
+ break ;
178
+ }
179
+ return move_speed_dir . normalized ;
180
+ }
181
+ }
0 commit comments