@@ -11,8 +11,50 @@ public class ZOSpotCharacterController : MonoBehaviour {
1111
1212 public Rigidbody _rigidBody ;
1313 public Transform _frontCollider = null ;
14+
15+ public CapsuleCollider FrontColliderCapsule {
16+ get {
17+ return _frontCollider ? . GetComponent < CapsuleCollider > ( ) ;
18+ }
19+ }
20+
21+ public Rigidbody FrontColliderRigidBody {
22+ get {
23+ return _frontCollider ? . GetComponent < Rigidbody > ( ) ;
24+ }
25+ }
26+
27+ public ZOGroundDetector FrontGroundDetector {
28+ get {
29+ return _frontCollider ? . GetComponent < ZOGroundDetector > ( ) ;
30+ }
31+ }
1432 public Transform _rearCollider = null ;
33+ public CapsuleCollider RearColliderCapsule {
34+ get {
35+ return _rearCollider ? . GetComponent < CapsuleCollider > ( ) ;
36+ }
37+ }
38+
39+ public ZOGroundDetector RearGroundDetector {
40+ get {
41+ return _rearCollider ? . GetComponent < ZOGroundDetector > ( ) ;
42+ }
43+ }
44+
45+ public Rigidbody RearColliderRigidBody {
46+ get {
47+ return _rearCollider ? . GetComponent < Rigidbody > ( ) ;
48+ }
49+ }
50+
51+
1552 public float _maxFloorAngle = 45 ;
53+
54+ public float MaxFloorAngle {
55+ get => _maxFloorAngle ;
56+ set => _maxFloorAngle = value ;
57+ }
1658 public float _maxForwardVelocity = 1.0f ;
1759
1860 public float MaxForwardVelocity {
@@ -34,10 +76,41 @@ public float MaxTurnVelocityDegreesSecond {
3476 private Vector2 _targetVelocity = Vector2 . zero ;
3577 private float _targetTurnVelocityDegreesPerSecond = 0.0f ;
3678
79+ private float _lieDownScale = 0.5f ;
80+
81+ private bool IsFrontGrounded {
82+ get {
83+ return FrontGroundDetector . IsGrounded ;
84+ }
85+ }
86+
87+ private bool IsRearGrounded {
88+ get {
89+ return RearGroundDetector . IsGrounded ;
90+ }
91+ }
92+
93+
94+ private Vector3 FrontGroundNormal {
95+ get {
96+ return FrontGroundDetector . GroundNormal ;
97+ }
98+ }
99+
100+
101+ private Vector3 RearGroundNormal {
102+ get {
103+ return RearGroundDetector . GroundNormal ;
104+ }
105+ }
106+
107+
108+
37109 public enum StateEnum {
38110 Off ,
39- Down ,
111+ Sitting ,
40112 UpAndReady ,
113+ Busy ,
41114 Error
42115 }
43116
@@ -60,46 +133,105 @@ public ZOSpotCharacterController.ErrorEnum Error {
60133
61134
62135 public delegate void SpotControllerStatusDelegate ( ZOSpotCharacterController thisClass , Vector2 linearVelocity , float turnVelocityDegreesPerSecond , float orientationDegrees , ZOSpotCharacterController . StateEnum state , ZOSpotCharacterController . ErrorEnum error ) ;
63- private event SpotControllerStatusDelegate _spotContrllerStatusDelegate ;
136+ private event SpotControllerStatusDelegate _spotControllerStatusDelegate ;
64137 public event SpotControllerStatusDelegate SpotControllerStatus {
65138 add {
66- _spotContrllerStatusDelegate += value ;
139+ _spotControllerStatusDelegate += value ;
67140 }
68141 remove {
69- _spotContrllerStatusDelegate -= value ;
142+ _spotControllerStatusDelegate -= value ;
70143 }
71144 }
72145
73146
74147
75148 // Start is called before the first frame update
76149 void Start ( ) {
77-
150+ // start sitting down
151+ Sit ( ) ;
78152 }
79153
80154 // Update is called once per frame
81155 void Update ( ) {
82156
83157 }
84158
159+
160+
85161 void FixedUpdate ( ) {
86162
87- _rigidBody . velocity = _rigidBody . transform . rotation * new Vector3 ( _targetVelocity . x , _rigidBody . velocity . y , _targetVelocity . y ) ;
163+ if ( IsFrontGrounded || IsRearGrounded ) {
164+
165+ Vector3 angularVelocity = _rigidBody . angularVelocity ;
166+ float torqueYaw = ( _targetTurnVelocityDegreesPerSecond * Mathf . Deg2Rad ) - angularVelocity . y ;
167+ _rigidBody . AddRelativeTorque ( new Vector3 ( 0 , torqueYaw , 0 ) , ForceMode . VelocityChange ) ;
88168
89- Vector3 angularVelocity = _rigidBody . angularVelocity ;
90- angularVelocity . y = _targetTurnVelocityDegreesPerSecond * Mathf . Deg2Rad ;
91- _rigidBody . angularVelocity = angularVelocity ;
169+ Vector3 localVelocity = _rigidBody . transform . InverseTransformDirection ( _rigidBody . velocity ) ;
170+ Vector3 targetVelocity = new Vector3 ( _targetVelocity . x , 0 , _targetVelocity . y ) ;
171+ Vector3 deltaVelocity = targetVelocity - localVelocity ;
172+ deltaVelocity = _rigidBody . transform . TransformDirection ( deltaVelocity ) ;
173+ _rigidBody . AddForce ( deltaVelocity , ForceMode . VelocityChange ) ;
92174
93- if ( _spotContrllerStatusDelegate != null ) {
94- _spotContrllerStatusDelegate . Invoke ( this , new Vector2 ( _rigidBody . velocity . x , _rigidBody . velocity . z ) , _rigidBody . angularVelocity . y * Mathf . Rad2Deg , transform . rotation . eulerAngles . y , State , Error ) ;
95175
96176 }
177+
178+
179+ if ( _spotControllerStatusDelegate != null ) {
180+ _spotControllerStatusDelegate . Invoke ( this , new Vector2 ( _rigidBody . velocity . x , _rigidBody . velocity . z ) , _rigidBody . angularVelocity . y * Mathf . Rad2Deg , transform . rotation . eulerAngles . y , State , Error ) ;
181+
182+ }
183+
184+ // obey gravity
185+ if ( IsFrontGrounded == true ) {
186+ Vector3 gravity = - FrontGroundNormal * UnityEngine . Physics . gravity . magnitude * 20.2f ;
187+ FrontColliderRigidBody . AddForce ( gravity , ForceMode . Acceleration ) ;
188+ } else {
189+ FrontColliderRigidBody . AddForce ( UnityEngine . Physics . gravity * 2.2f , ForceMode . Acceleration ) ;
190+ }
191+
192+ if ( IsRearGrounded == true ) {
193+ Vector3 gravity = - RearGroundNormal * UnityEngine . Physics . gravity . magnitude * 20.2f ;
194+ RearColliderRigidBody . AddForce ( gravity , ForceMode . Acceleration ) ;
195+ } else {
196+ RearColliderRigidBody . AddForce ( UnityEngine . Physics . gravity * 2.2f , ForceMode . Acceleration ) ;
197+ }
198+
199+ // if (IsFrontGrounded == false || IsRearGrounded == false) {
200+ // _rigidBody.AddForce(UnityEngine.Physics.gravity, ForceMode.Acceleration);
201+ // }
202+
203+ }
204+
205+
206+
207+
208+ public void TurnOn ( ) {
209+
210+ }
211+
212+ public void Stand ( ) {
213+ if ( State == ZOSpotCharacterController . StateEnum . Sitting ) {
214+ FrontColliderCapsule . height = FrontColliderCapsule . height * ( 1.0f / _lieDownScale ) ;
215+ RearColliderCapsule . height = RearColliderCapsule . height * ( 1.0f / _lieDownScale ) ;
216+ State = ZOSpotCharacterController . StateEnum . UpAndReady ;
217+ }
218+ }
219+
220+ public void Sit ( ) {
221+ if ( State == ZOSpotCharacterController . StateEnum . UpAndReady || State == ZOSpotCharacterController . StateEnum . Off ) {
222+ FrontColliderCapsule . height = FrontColliderCapsule . height * _lieDownScale ;
223+ RearColliderCapsule . height = RearColliderCapsule . height * _lieDownScale ;
224+ State = ZOSpotCharacterController . StateEnum . Sitting ;
225+ }
226+
97227 }
98228
99229
100230 public void Move ( Vector2 targetVelocity , float targetTurnVelocityDegreesPerSecond ) {
101- _targetVelocity = targetVelocity ;
102- _targetTurnVelocityDegreesPerSecond = targetTurnVelocityDegreesPerSecond ;
231+ if ( State == ZOSpotCharacterController . StateEnum . UpAndReady ) {
232+ _targetVelocity = targetVelocity ;
233+ _targetTurnVelocityDegreesPerSecond = targetTurnVelocityDegreesPerSecond ;
234+ }
103235 }
104236 // void Sit(Delegate OnSitFinishedOrError)
105237 // void Stand(Delegate OnStandFinishedOrError)
0 commit comments