質問するログイン新規登録

質問編集履歴

6

情報の追加・修正

2020/04/22 13:16

投稿

RO_456
RO_456

スコア2

title CHANGED
File without changes
body CHANGED
@@ -8,10 +8,11 @@
8
8
 
9
9
  [動画のように、発射した後に左右に自機を動かすと弾も動いてしまう。](https://www.youtube.com/watch?v=Xb0LpWj0OLU)
10
10
  [b.transform.parent= bulletParent;を外した場合](https://www.youtube.com/watch?v=cLhGiqF3Gcg)
11
- エラーメッセージ
12
- ```
13
11
 
12
+
13
+
14
14
  ### 該当のソースコード
15
+ ```
15
16
  using System.Collections;
16
17
  using System.Collections.Generic;
17
18
  using UnityEngine;
@@ -50,4 +51,204 @@
50
51
 
51
52
  }
52
53
 
53
- }
54
+ }
55
+ ```
56
+ ### 追記
57
+ 質問への追記・修正の依頼ありがとうございます。
58
+ このプロジェクトは[StarFox-RailMovement](https://github.com/mixandjam/StarFox-RailMovement)というツールを使用したものとなっています。
59
+ 恐らくはmuzzleもPlayerの子にあるため、つまりはPlayerとしての挙動を得てしまっているため、このようなバラバラの挙動になるものと思われます。
60
+
61
+ ![イメージ説明](9f8a0b3b6de919743c14f82ec98b1f96.jpeg)
62
+ 親の層にある「Player」が、Playerを動かすPlayerMove.csがアタッチされています。
63
+ 以下、PlayerMove.csの内容です。
64
+ ### PlayerMove.cs
65
+ ```
66
+ using System.Collections;
67
+ using System.Collections.Generic;
68
+ using UnityEngine;
69
+ using DG.Tweening;
70
+ using Cinemachine;
71
+ //using UnityEngine.Rendering.PostProcessing;
72
+
73
+ public class PlayerMovement : MonoBehaviour
74
+ {
75
+ private Transform playerModel;
76
+
77
+ [Header("Settings")]
78
+ public bool joystick = true;
79
+
80
+ [Space]
81
+
82
+ [Header("Parameters")]
83
+ public float xySpeed = 18;
84
+ public float lookSpeed = 340;
85
+ public float forwardSpeed = 6;
86
+
87
+ [Space]
88
+
89
+ [Header("Public References")]
90
+ public Transform aimTarget;
91
+ public CinemachineDollyCart dolly;
92
+ public Transform cameraParent;
93
+
94
+ [Space]
95
+
96
+ [Header("Particles")]
97
+ public ParticleSystem trail;
98
+ public ParticleSystem circle;
99
+ public ParticleSystem barrel;
100
+ public ParticleSystem stars;
101
+
102
+ void Start()
103
+ {
104
+ playerModel = transform.GetChild(0);
105
+ SetSpeed(forwardSpeed);
106
+ }
107
+
108
+ void Update()
109
+ {
110
+ float h = joystick ? Input.GetAxis("Horizontal") : Input.GetAxis("Mouse X");
111
+ float v = joystick ? Input.GetAxis("Vertical") : Input.GetAxis("Mouse Y");
112
+
113
+ LocalMove(h, v, xySpeed);
114
+ RotationLook(h,v, lookSpeed);
115
+ HorizontalLean(playerModel, h, 80, .1f);
116
+
117
+ if (Input.GetButtonDown("Action"))
118
+ Boost(true);
119
+
120
+ if (Input.GetButtonUp("Action"))
121
+ Boost(false);
122
+
123
+ if (Input.GetButtonDown("Fire3"))
124
+ Break(true);
125
+
126
+ if (Input.GetButtonUp("Fire3"))
127
+ Break(false);
128
+
129
+ if (Input.GetButtonDown("TriggerL") || Input.GetButtonDown("TriggerR"))
130
+ {
131
+ int dir = Input.GetButtonDown("TriggerL") ? -1 : 1;
132
+ QuickSpin(dir);
133
+ }
134
+
135
+
136
+ }
137
+
138
+ void LocalMove(float x, float y, float speed)
139
+ {
140
+ transform.localPosition += new Vector3(x, y, 0) * speed * Time.deltaTime;
141
+ ClampPosition();
142
+ }
143
+
144
+ void ClampPosition()
145
+ {
146
+ Vector3 pos = Camera.main.WorldToViewportPoint(transform.position);
147
+ pos.x = Mathf.Clamp01(pos.x);
148
+ pos.y = Mathf.Clamp01(pos.y);
149
+ transform.position = Camera.main.ViewportToWorldPoint(pos);
150
+ }
151
+
152
+ void RotationLook(float h, float v, float speed)
153
+ {
154
+ aimTarget.parent.position = Vector3.zero;
155
+ aimTarget.localPosition = new Vector3(h, v, 1);
156
+ transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(aimTarget.position), Mathf.Deg2Rad * speed * Time.deltaTime);
157
+ }
158
+
159
+ void HorizontalLean(Transform target, float axis, float leanLimit, float lerpTime)
160
+ {
161
+ Vector3 targetEulerAngels = target.localEulerAngles;
162
+ target.localEulerAngles = new Vector3(targetEulerAngels.x, targetEulerAngels.y, Mathf.LerpAngle(targetEulerAngels.z, -axis * leanLimit, lerpTime));
163
+ }
164
+
165
+ private void OnDrawGizmos()
166
+ {
167
+ Gizmos.color = Color.blue;
168
+ Gizmos.DrawWireSphere(aimTarget.position, .5f);
169
+ Gizmos.DrawSphere(aimTarget.position, .15f);
170
+
171
+ }
172
+
173
+ public void QuickSpin(int dir)
174
+ {
175
+ if (!DOTween.IsTweening(playerModel))
176
+ {
177
+ playerModel.DOLocalRotate(new Vector3(playerModel.localEulerAngles.x, playerModel.localEulerAngles.y, 360 * -dir), .4f, RotateMode.LocalAxisAdd).SetEase(Ease.OutSine);
178
+ barrel.Play();
179
+ }
180
+ }
181
+
182
+ void SetSpeed(float x)
183
+ {
184
+ dolly.m_Speed = x;
185
+ }
186
+
187
+ void SetCameraZoom(float zoom, float duration)
188
+ {
189
+ cameraParent.DOLocalMove(new Vector3(0, 0, zoom), duration);
190
+ }
191
+
192
+ void DistortionAmount(float x)
193
+ {
194
+ // Camera.main.GetComponent<PostProcessVolume>().profile.GetSetting<LensDistortion>().intensity.value = x;
195
+ }
196
+
197
+ void FieldOfView(float fov)
198
+ {
199
+ cameraParent.GetComponentInChildren<CinemachineVirtualCamera>().m_Lens.FieldOfView = fov;
200
+ }
201
+
202
+ void Chromatic(float x)
203
+ {
204
+ // Camera.main.GetComponent<PostProcessVolume>().profile.GetSetting<ChromaticAberration>().intensity.value = x;
205
+ }
206
+
207
+
208
+ void Boost(bool state)
209
+ {
210
+
211
+ if (state)
212
+ {
213
+ cameraParent.GetComponentInChildren<CinemachineImpulseSource>().GenerateImpulse();
214
+ trail.Play();
215
+ circle.Play();
216
+ }
217
+ else
218
+ {
219
+ trail.Stop();
220
+ circle.Stop();
221
+ }
222
+ trail.GetComponent<TrailRenderer>().emitting = state;
223
+
224
+ float origFov = state ? 40 : 55;
225
+ float endFov = state ? 55 : 40;
226
+ float origChrom = state ? 0 : 1;
227
+ float endChrom = state ? 1 : 0;
228
+ float origDistortion = state ? 0 : -30;
229
+ float endDistorton = state ? -30 : 0;
230
+ float starsVel = state ? -20 : -1;
231
+ float speed = state ? forwardSpeed * 2 : forwardSpeed;
232
+ float zoom = state ? -7 : 0;
233
+
234
+ DOVirtual.Float(origChrom, endChrom, .5f, Chromatic);
235
+ DOVirtual.Float(origFov, endFov, .5f, FieldOfView);
236
+ DOVirtual.Float(origDistortion, endDistorton, .5f, DistortionAmount);
237
+ var pvel = stars.velocityOverLifetime;
238
+ pvel.z = starsVel;
239
+
240
+ DOVirtual.Float(dolly.m_Speed, speed, .15f, SetSpeed);
241
+ SetCameraZoom(zoom, .4f);
242
+ }
243
+
244
+ void Break(bool state)
245
+ {
246
+ float speed = state ? forwardSpeed / 3 : forwardSpeed;
247
+ float zoom = state ? 3 : 0;
248
+
249
+ DOVirtual.Float(dolly.m_Speed, speed, .15f, SetSpeed);
250
+ SetCameraZoom(zoom, .4f);
251
+ }
252
+ }
253
+
254
+ ```

5

誤字の修正

2020/04/22 13:16

投稿

RO_456
RO_456

スコア2

title CHANGED
File without changes
body CHANGED
@@ -1,6 +1,6 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
- 3Dシューティングゲームを作っているのですが、弾丸を発射すると下記の動画のように自機を左右に移動ると弾も左右に移動してしまいます。
3
+ 3Dシューティングゲームを作っているのですが、弾丸を発射した後、下記の動画のように自機を移動させると弾も左右に移動してしまいます。
4
4
  真っ直ぐ飛ぶようにしたいのですが、ソースコードのb.transform.parent= bulletParent;を外すとPlayerの動きの都合上、大きくマズルがずれてしまいます。
5
5
  代替策か、左右への移動を無効にする方法を知りたいです。
6
6
 

4

誤字の編集

2020/04/22 12:10

投稿

RO_456
RO_456

スコア2

title CHANGED
File without changes
body CHANGED
@@ -1,6 +1,6 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
- 3Dシューティングゲームを作っているのですが、弾丸を発射すると下記の動画のようにを左右に移動してしまいます。
3
+ 3Dシューティングゲームを作っているのですが、弾丸を発射すると下記の動画のように自機を左右に移動すると弾も左右に移動してしまいます。
4
4
  真っ直ぐ飛ぶようにしたいのですが、ソースコードのb.transform.parent= bulletParent;を外すとPlayerの動きの都合上、大きくマズルがずれてしまいます。
5
5
  代替策か、左右への移動を無効にする方法を知りたいです。
6
6
 

3

タグの編集

2020/04/22 12:09

投稿

RO_456
RO_456

スコア2

title CHANGED
File without changes
body CHANGED
File without changes

2

文法の修正

2020/04/22 12:04

投稿

RO_456
RO_456

スコア2

title CHANGED
File without changes
body CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  ### 発生している問題・エラーメッセージ
8
8
 
9
- [以下の動画のように、発射した後に左右に自機を動かすと弾も動いてしまう。](https://www.youtube.com/watch?v=Xb0LpWj0OLU)
9
+ [動画のように、発射した後に左右に自機を動かすと弾も動いてしまう。](https://www.youtube.com/watch?v=Xb0LpWj0OLU)
10
10
  [b.transform.parent= bulletParent;を外した場合](https://www.youtube.com/watch?v=cLhGiqF3Gcg)
11
11
  エラーメッセージ
12
12
  ```

1

誤字を訂正

2020/04/22 12:02

投稿

RO_456
RO_456

スコア2

title CHANGED
File without changes
body CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  3Dシューティングゲームを作っているのですが、弾丸を発射すると下記の動画のように弾を左右に移動してしまいます。
4
4
  真っ直ぐ飛ぶようにしたいのですが、ソースコードのb.transform.parent= bulletParent;を外すとPlayerの動きの都合上、大きくマズルがずれてしまいます。
5
- 左右への移動を無効にする方法。
5
+ 代替策か、左右への移動を無効にする方法を知りたいです
6
6
 
7
7
  ### 発生している問題・エラーメッセージ
8
8