summaryrefslogtreecommitdiff
path: root/Assets/Scripts/GamePlay/Player/ChairMovement.cs
blob: f254c97a81c3183b8be965c1acded670fbd558f9 (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
using System;
using UnityEngine;
using UnityEngine.InputSystem;

namespace GamePlay.Player
{
    [RequireComponent(typeof(PlayerInput))]
    [RequireComponent(typeof(Rigidbody))]
    public class ChairMovement : MonoBehaviour
    {
        private PlayerInput _playerInput;
        private Rigidbody _rigidbody;

        private float _inputHorizontal;
        private float _inputVertical;
        
        public float maxMotorTorque = 400f;
        public float maxSteerAngle = 90f;
        public float brakeTorque = 1000f;
        
        public WheelCollider frontLeftWheel;
        public WheelCollider frontRightWheel;
        public WheelCollider rearLeftWheel;
        public WheelCollider rearRightWheel;

        private void Awake()
        {
            _playerInput = GetComponent<PlayerInput>();
            _rigidbody = GetComponent<Rigidbody>();

            _playerInput.onActionTriggered += OnActionTriggered;
        }

        private void OnActionTriggered(InputAction.CallbackContext ctx)
        {
            if (ctx.action.name == "Move")
            {
                var moveInput = ctx.ReadValue<Vector2>();
                _inputHorizontal = moveInput.x;
                _inputVertical = moveInput.y;
            }
        }

        private void FixedUpdate()
        {
            ApplySteering();
            ApplyMotor();
            ApplyBrakes();
        }

        private void ApplySteering()
        {
            float steer = maxSteerAngle * _inputHorizontal;
            frontLeftWheel.steerAngle = steer;
            frontRightWheel.steerAngle = steer;
        }

        private void ApplyMotor()
        {
            if (_inputVertical > 0)
            {
                float motor = maxMotorTorque * _inputVertical;
                OperateWheel(w => w.motorTorque = motor);

                // 释放刹车(因为在前进)
                OperateWheel(w => w.brakeTorque = 0);
            }
        }

        private void ApplyBrakes()
        {
            if (_inputVertical < 0)
            {
                if (_rigidbody.linearVelocity.z > 0.1f)
                {
                    float brake = brakeTorque * Mathf.Abs(_inputVertical);
                    OperateWheel(w => w.brakeTorque = brake);
                    OperateWheel(w => w.motorTorque = 0);
                }
                else
                {
                    OperateWheel(w => w.brakeTorque = 0);
                    OperateWheel(w => w.motorTorque = maxMotorTorque * _inputVertical);
                }
            }
            else if (Mathf.Abs(_inputVertical) < 0.1f)
            {
                float parkingBrake = 10f;
                OperateWheel(w => w.brakeTorque = parkingBrake);
                OperateWheel(w => w.motorTorque = 0);
            }
        }

        private void OperateWheel(Action<WheelCollider> o)
        {
            o(frontLeftWheel);
            o(frontRightWheel);
            o(frontLeftWheel);
            o(frontRightWheel);
        }
    }
}