回答編集履歴

1

回答検討中に使用していたネームスペースが残ったままだったが、回答コードに残したままでは余計な混乱を招きそうに思ったため削除

2022/06/13 12:27

投稿

Bongo
Bongo

スコア10807

test CHANGED
@@ -53,74 +53,71 @@
53
53
  using Cinemachine;
54
54
  using UnityEngine;
55
55
 
56
+ [RequireComponent(typeof(Rigidbody))]
56
- namespace DollyCart
57
+ public class Player : MonoBehaviour
57
58
  {
59
+ [SerializeField] private CinemachineDollyCart dollyCart;
60
+
61
+ private float force;
58
- [RequireComponent(typeof(Rigidbody))]
62
+ private new Rigidbody rigidbody;
63
+
59
- public class Player : MonoBehaviour
64
+ private void Awake()
60
65
  {
66
+ // インスペクター上でdollyCartをセットしなかった場合、
61
- [SerializeField] private CinemachineDollyCart dollyCart;
67
+ // 親子階層の中からCinemachineDollyCartを探すことにしました
62
-
63
- private float force;
64
- private new Rigidbody rigidbody;
68
+ if (this.dollyCart == null)
65
-
66
- private void Awake()
67
69
  {
68
- // インスペクター上でdollyCartをセットしなかった場合、
69
- // 親子階層の中からCinemachineDollyCartを探すことにしました
70
+ this.dollyCart = this.GetComponentInParent<CinemachineDollyCart>();
70
71
  if (this.dollyCart == null)
71
72
  {
72
- this.dollyCart = this.GetComponentInParent<CinemachineDollyCart>();
73
+ this.dollyCart = this.GetComponentInChildren<CinemachineDollyCart>();
73
74
  if (this.dollyCart == null)
74
75
  {
75
- this.dollyCart = this.GetComponentInChildren<CinemachineDollyCart>();
76
- if (this.dollyCart == null)
77
- {
78
- Debug.LogError($"{nameof(CinemachineDollyCart)} not found.");
76
+ Debug.LogError($"{nameof(CinemachineDollyCart)} not found.");
79
- this.enabled = false;
77
+ this.enabled = false;
80
- return;
78
+ return;
81
- }
82
79
  }
83
80
  }
84
-
85
- this.rigidbody = this.GetComponent<Rigidbody>();
86
- this.rigidbody.isKinematic = true;
87
81
  }
88
82
 
83
+ this.rigidbody = this.GetComponent<Rigidbody>();
84
+ this.rigidbody.isKinematic = true;
85
+ }
86
+
89
- private void FixedUpdate()
87
+ private void FixedUpdate()
88
+ {
89
+ this.force = 0.0f;
90
+ if (Input.GetKey(KeyCode.W))
90
91
  {
91
- this.force = 0.0f;
92
- if (Input.GetKey(KeyCode.W))
93
- {
94
- this.force += 1;
92
+ this.force += 1;
95
- }
96
-
97
- if (Input.GetKey(KeyCode.S))
98
- {
99
- this.force -= 1;
100
- }
101
-
102
- // オブジェクトの質量を得るためにrigidbodyを使用していますが、
103
- // 動きはdollyCartによってコントロールされており、rigidbodyは関与していません
104
- // もし質量が決め打ちでかまわなければ、あるいはForceModeが質量の影響を
105
- // 受けないタイプでもかまわなければ、この質量の取得すら省くことができますので
106
- // 完全にdollyCartだけで動かすことができるでしょう
107
- this.dollyCart.AddForce(this.force, ForceMode.Force, this.rigidbody.mass); // 力をon!
108
-
109
- // ちなみに「sキーを押して減速、その後停止するようにしたい。」と
110
- // おっしゃっているということは、sキーでだんだん減速していった後
111
- // さらにsキーを押し続けてバックし始めてしまうとまずいということでしょうか?
112
- // その場合は、下記のように第4引数をfalseにするとバックしなくなるはずです
113
- // this.dollyCart.AddForce(this.force, ForceMode.Force, this.rigidbody.mass, false);
114
93
  }
115
94
 
116
- private void OnGUI()
95
+ if (Input.GetKey(KeyCode.S))
117
96
  {
118
- // 確認のため、現在カートにトラックに沿って加えられている力と
119
- // カートのトラックに沿った速度を表示させてみました
120
- GUI.color = Color.black;
97
+ this.force -= 1;
121
- GUILayout.Label($"Force: {this.force}");
122
- GUILayout.Label($"Speed: {this.dollyCart.m_Speed}");
123
98
  }
99
+
100
+ // オブジェクトの質量を得るためにrigidbodyを使用していますが、
101
+ // 動きはdollyCartによってコントロールされており、rigidbodyは関与していません
102
+ // もし質量が決め打ちでかまわなければ、あるいはForceModeが質量の影響を
103
+ // 受けないタイプでもかまわなければ、この質量の取得すら省くことができますので
104
+ // 完全にdollyCartだけで動かすことができるでしょう
105
+ this.dollyCart.AddForce(this.force, ForceMode.Force, this.rigidbody.mass); // 力をon!
106
+
107
+ // ちなみに「sキーを押して減速、その後停止するようにしたい。」と
108
+ // おっしゃっているということは、sキーでだんだん減速していった後
109
+ // さらにsキーを押し続けてバックし始めてしまうとまずいということでしょうか?
110
+ // その場合は、下記のように第4引数をfalseにするとバックしなくなるはずです
111
+ // this.dollyCart.AddForce(this.force, ForceMode.Force, this.rigidbody.mass, false);
112
+ }
113
+
114
+ private void OnGUI()
115
+ {
116
+ // 確認のため、現在カートにトラックに沿って加えられている力と
117
+ // カートのトラックに沿った速度を表示させてみました
118
+ GUI.color = Color.black;
119
+ GUILayout.Label($"Force: {this.force}");
120
+ GUILayout.Label($"Speed: {this.dollyCart.m_Speed}");
124
121
  }
125
122
  }
126
123
  ```