11using System ;
2+ using System . Collections ;
23using UnityEngine ;
34
45namespace Player {
56 public class ChainManager : MonoBehaviour {
67 [ SerializeField ] private GameObject _link ;
78 [ SerializeField ] private int _length ;
8- [ SerializeField ] private Vector3 _offset ;
99 [ SerializeField ] private float _linkLength = 1 ;
1010
1111 [ SerializeField ] private Rigidbody _from ;
1212 [ SerializeField ] private Rigidbody _to ;
13+ [ SerializeField ] private Vector3 [ ] _positions ;
14+ [ SerializeField ] private Quaternion [ ] _rotations ;
1315
1416 private Rigidbody [ ] _links ;
1517
1618 private void Awake ( ) {
19+ #if UNITY_EDITOR
20+ if ( _positions . Length < _length ) {
21+ _positions = new Vector3 [ _length ] ;
22+ }
23+
24+ if ( _rotations . Length < _length ) {
25+ _rotations = new Quaternion [ _length ] ;
26+ }
27+ #endif
28+
1729 _links = new Rigidbody [ _length ] ;
1830 var prev = Instantiate ( _link ) ;
19- prev . transform . position = _from . position ;
31+ prev . transform . position = _positions [ 0 ] ;
32+ prev . transform . rotation = _rotations [ 0 ] ;
2033 _links [ 0 ] = prev . GetComponent < Rigidbody > ( ) ;
2134
2235 var joint = _to . GetComponent < Joint > ( ) ;
@@ -30,7 +43,8 @@ private void Awake() {
3043
3144 for ( var i = 1 ; i < _length ; i ++ ) {
3245 var link = Instantiate ( _link ) ;
33- link . transform . position = _from . position + _offset * i ;
46+ link . transform . position = _positions [ i ] ;
47+ link . transform . rotation = _rotations [ i ] ;
3448 joint = prev . GetComponent < Joint > ( ) ;
3549 _links [ i ] = link . GetComponent < Rigidbody > ( ) ;
3650 joint . connectedBody = _links [ i ] ;
@@ -55,5 +69,18 @@ private void Awake() {
5569 anchor . y = _linkLength ;
5670 joint . connectedAnchor = anchor ;
5771 }
72+
73+ #if UNITY_EDITOR
74+ [ ContextMenu ( "Store Chain Transform" ) ]
75+ private void StoreChainTransform ( ) {
76+ _positions = new Vector3 [ _length ] ;
77+ _rotations = new Quaternion [ _length ] ;
78+ for ( var i = 0 ; i < _length ; i ++ ) {
79+ var link = _links [ i ] . transform ;
80+ _positions [ i ] = link . position ;
81+ _rotations [ i ] = link . rotation ;
82+ }
83+ }
84+ #endif
5885 }
5986}
0 commit comments