aboutsummaryrefslogtreecommitdiff
path: root/JamHelper/Assets/JamTools/FPSControllerVelocity/Scripts/FPSCharacterController.cs
blob: c51fffc55b1984f714adb1bdf2de0db9a5eefc0f (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace JamTools
{

    // 第一人称角色控制
    public class FPSCharacterController : MonoBehaviour
    {
        [Flags]
        public enum CharacterModule
        {
            None,
            LookAround = 1 << 0,  // 相机环绕
            MoveAround = 1 << 1,  // 水平地面移动
            MoveInAir = 1 << 2,  // 空中移动
            Dodge = 1 << 3,  // 冲刺
            Rush = 1 << 4,  // 加速
            WalkOnSlope = 1 << 5 | 1 << 1,  // 斜面移动
            WalkOnStairs = 1 << 6 | 1 << 1,  // 楼梯移动
            WallRun = 1 << 7,  // 飞檐走壁
            Jump = 1 << 8,  // 跳跃
            WallJump = 1 << 9,  // 忍者跳
            Slide = 1 << 10, // 滑铲
            Shot = 1 << 11, // 射击
            Step = 1 << 12, // 脚步
            PullTrick = 1 << 13, // 
            ExtraGravity = 1 << 14,  // 额外重力
            Crouch = 1 << 15,
        }

        [SerializeField] private CharacterModule m_Modules;

        [SerializeField] private Transform m_Eye;

        [SerializeField] private PlayerBody m_Body;
        [SerializeField] private GroundChecker m_GroundChecker;
        [SerializeField] private WallChecker m_WallChecker;

        [SerializeField] private MainCameraFollow m_Camera;

        #region Modules

        [Header("Look Around")]
        [SerializeField] private float m_LookSensitive = 1000f;
        [Range(0.01f, 1)]
        [SerializeField] private float m_LookSmooth = 0.2f;
        private float m_CameraRotation;
        private float m_BodyRotation;

        [Header("Move Around")]
        [SerializeField] private float m_MoveSpeed = 100f;
        [Range(0.01f, 1)]
        [SerializeField] private float m_MoveSmooth = 0.2f;
        private Vector3 m_MoveDirection;
        private bool m_ReadyMoveAround = false;

        [Header("Move In Air")]
        [SerializeField] private float m_MoveSpeedInAir = 50f;
        [SerializeField] private float m_MoveInAirSmooth = 0.2f;
        private Vector3 m_MoveInAirDirection;

        [Header("Jump")]
        [SerializeField] private float m_JumpPower = 300f;
        private bool m_ReadyToJump = false;

        [Header("Shot")]
        [SerializeField] private Transform m_Muzzle;
        [SerializeField] private LayerMask m_HittableLayers;
        [SerializeField] private float m_ShotInfiniteDistance = 100f;

        [Header("WallJump")]
        [SerializeField] private float m_WallJumpForce = 1;
		[SerializeField] private float m_WallJumpPower = 1000;
        private bool m_ReadyWallJump = false;

        [Header("WallRun")]
        [SerializeField] private float m_WallRunSpeed = 1;

		[Header("ExtraGravity")]
        [SerializeField] private Vector3 m_ExtraGravity;

        #endregion 

        public Func<RaycastHit, bool> checkHit;
        public Action<Vector3, Transform> shootTarget;

        private Rigidbody m_Rigidbody;

        private bool m_LockCursor = false;
        public bool lockCursor
        {
            get
            {
                return m_LockCursor;
            }
            set
            {
                m_LockCursor = value;
                if (value)
                {
                    Cursor.lockState = CursorLockMode.Locked;
                }
                else
                {
                    Cursor.lockState = CursorLockMode.None;
                }
            }
        }

        bool IsModuleActive(CharacterModule module)
        {
            return (module & m_Modules) != 0;
        }

        void ActivateModule(CharacterModule module)
        {
            m_Modules |= module;
        }

        void LookAround()
        {
            if (!IsModuleActive(CharacterModule.LookAround))
                return;

            float mouseX = Input.GetAxis("Mouse X");
            float mouseY = Input.GetAxis("Mouse Y");

            m_CameraRotation -= mouseY * Time.deltaTime * m_LookSensitive;
            m_CameraRotation = Mathf.Clamp(m_CameraRotation, -90, 90);
            Quaternion rot = Quaternion.Euler(m_CameraRotation, 0, 0);
            m_Eye.localRotation = Quaternion.Slerp(m_Eye.localRotation, rot, m_LookSmooth );

            m_BodyRotation += mouseX * Time.deltaTime * m_LookSensitive;
            rot = Quaternion.Euler(0, m_BodyRotation, 0);
            transform.localRotation = Quaternion.Slerp(transform.localRotation, rot, m_LookSmooth);
        }

        void MoveAroundUpdate()
        {
            if (!IsModuleActive(CharacterModule.MoveAround))
                return;

            if (!m_GroundChecker.isOnGround)
                return;

            float moveX = Input.GetAxisRaw("Horizontal");
            float moveZ = Input.GetAxisRaw("Vertical");

            Vector3 right = transform.right;
            Vector3 forward = transform.forward;

            m_MoveDirection = right * moveX + forward * moveZ;

            if (IsModuleActive(CharacterModule.WalkOnSlope))
            {
                if (m_GroundChecker.isOnGround)
                {
                    RaycastHit hitInfo;
                    if (Physics.Raycast(m_GroundChecker.foot.position, Vector3.down, out hitInfo))
                    {
                        Vector3 normal = hitInfo.normal;
                        m_MoveDirection = Vector3.ProjectOnPlane(m_MoveDirection, normal);
                        GizmosHandle.Instance.DoGizmos(() =>
                        {
                            Gizmos.DrawLine(hitInfo.point + new Vector3(0, 0.1f, 0), hitInfo.point + m_MoveDirection);
                        });
                    }
                }
            }

            m_MoveDirection = m_MoveDirection.normalized;

        }

        void MoveAroundFixedUpdate()
        {
            if (!IsModuleActive(CharacterModule.MoveAround))
                return;

            if (!m_GroundChecker.isOnGround)
                return;

            float vy = m_Rigidbody.velocity.y;
            Vector3 velocity = new Vector3(m_MoveDirection.x * Time.deltaTime * m_MoveSpeed, vy, m_MoveDirection.z * Time.deltaTime * m_MoveSpeed);

			m_Rigidbody.velocity = Vector3.Lerp(m_Rigidbody.velocity, velocity, m_MoveSmooth);
		}

        void MoveInAirUpdate()
        {
            if (!IsModuleActive(CharacterModule.MoveInAir))
                return;

            if (m_GroundChecker.isOnGround)
                return;

            float moveX = Input.GetAxisRaw("Horizontal");
            float moveZ = Input.GetAxisRaw("Vertical");

            m_MoveInAirDirection = Vector3.ClampMagnitude(transform.right * moveX + transform.forward * moveZ, 1);
        }

        void MoveInAirFixedUpdate()
        {
            if (!IsModuleActive(CharacterModule.MoveInAir))
                return;

            if (m_GroundChecker.isOnGround)
                return;

            if (m_MoveInAirDirection.magnitude == 0f)
                return;

            float vy = m_Rigidbody.velocity.y;
            Vector3 velocity = new Vector3(m_MoveInAirDirection.x * Time.deltaTime * m_MoveSpeedInAir, vy, m_MoveInAirDirection.z * Time.deltaTime * m_MoveSpeedInAir);

            m_Rigidbody.velocity = Vector3.Lerp(m_Rigidbody.velocity, velocity, m_MoveInAirSmooth);
        }

        void Jump()
        {
            if (!IsModuleActive(CharacterModule.Jump))
                return;

            if (!m_GroundChecker.isOnGround)
                return;

            if (Input.GetButtonDown("Jump"))
            {
                m_ReadyToJump = true;
            }
        }

        void JumpFixedUpdate()
        {
            if(m_ReadyToJump)
            {
                m_ReadyToJump = false;
                m_Rigidbody.AddForce(Vector3.up * m_JumpPower, ForceMode.Acceleration);
            }
        }

        void Dodge()
        {
            if (!IsModuleActive(CharacterModule.Dodge))
                return;

            if (Input.GetKeyDown(KeyCode.LeftShift))
            {

            }
        }

        void DodgeFixed()
        {

        }

        void Shot()
        {
            if (!IsModuleActive(CharacterModule.Shot))
                return;

            if (Input.GetButtonDown("Fire1"))
            {
                Vector3 hitPoint = GetHitPoint();
                if (shootTarget != null)
                    shootTarget(hitPoint, m_Muzzle);
            }
        }

        Vector3 GetHitPoint()
        {
            RaycastHit[] hits = Physics.RaycastAll(m_Eye.position, m_Eye.forward, m_HittableLayers, (int)QueryTriggerInteraction.Ignore);
            if (hits.Length < 1)
            {
                return m_Eye.position + m_Eye.forward * m_ShotInfiniteDistance;
            }
            else
            {
                for (int i = 0; i < hits.Length; ++i)
                {
                    if (checkHit != null && checkHit(hits[i]))
                    {
                        return hits[i].point;
                    }
                }
            }
            return hits[0].point;
        }

        void WallJump()
        {
			if (!IsModuleActive(CharacterModule.WallJump))
				return;

			if (!m_WallChecker.IsOnWall)
				return;

			if (m_GroundChecker.isOnGround)
				return;

			if (Input.GetButtonDown("Jump"))
			{
                m_ReadyWallJump = true;
			}
		}

        void WallJumpFixedUpdate()
        {
            if(m_ReadyWallJump)
            {
                m_ReadyWallJump = false;
                Vector3 poc;
                if (m_WallChecker.GetCollisionPoint(out poc))
                {
                    Vector3 dir = Vector3.ClampMagnitude(transform.position - poc, 1f);
                    Vector3 wallJumpDirection = new Vector3(dir.x, 1, dir.z);
                    m_Rigidbody.velocity = Vector3.zero;
                    m_Rigidbody.AddForce(wallJumpDirection * m_WallJumpPower);
                }
            }
        }

        void WallRun()
        {
            if (!IsModuleActive(CharacterModule.WallRun))
                return;

            if (!m_WallChecker.IsOnWall)
                return;

        }

        void PullTrick()
        {

        }

        void ExtraGravity()
        {
            if (!IsModuleActive(CharacterModule.ExtraGravity))
                return;

            m_Rigidbody.AddForce(m_ExtraGravity, ForceMode.Acceleration);
        }

        void SetCamera ()
        {
            m_Camera.SetCameraPositionAndRotation(m_Eye);
        }

        private void Awake()
		{
			m_Rigidbody = GetComponent<Rigidbody>();
		}

		private void Start()
		{
            m_CameraRotation = 0;

            lockCursor = true;
        }

		private void Update()
		{
            LookAround();
            MoveAroundUpdate();
            MoveInAirUpdate();
            Jump();
            Dodge();
            Shot();
            WallJump();
            PullTrick();

            SetCamera();
        }

		private void FixedUpdate()
		{
            MoveAroundFixedUpdate();
            MoveInAirFixedUpdate();
            DodgeFixed();
            ExtraGravity();
            JumpFixedUpdate();
            WallJumpFixedUpdate();
        }

        private void OnDrawGizmos()
        {

        }

    }

}