質問編集履歴

5

文章を修正

2022/04/13 05:51

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- [Unity3D] 子のオブジェクトの座標の変更方法で初期座標を一致させる方法が知りたい。
1
+ [Unity3D] 子のオブジェクトの座標初期座標に書き換える方法が知りたい。
test CHANGED
@@ -1,15 +1,14 @@
1
- 提示画像ですが青いオブジェクトが武器のスクリトです。白いオブジェクトがプレイヤースクリプトです。提示サイトのように武器を飛ばした方向にプレイヤーが武器の方向に飛んでいくという処理を実装しているのですがプレイヤー武器の方向飛んで行っときに武器の位置提示画像ような初期の座標に設定し直しですがこれをるにはどうすればいいのでしょうか?
1
+ 提示画像ですが武器を投げるとプレイヤー武器を投げた方向に飛んでいくのですが武器と同じ座標子のオブジェクトである青い武器がプレイヤー子の座標の初期座標つまり一枚目位置に座標を元戻す方法が知りです。提示コードの////コメント部内部該当コードで
2
- Player_Sword.cs下部の `WarpShiftEnd()`関数ですが最後はどのように`transform.position`を修正したらいいのでしょうか?
2
+
3
-
4
- ##### 試したこと
3
+
4
+
5
- 初期座標void Start() で保存して変更された時にその値を使って書き換えという処理行いましが上手く出来ませんでした。
5
+ ※提示画像1武器投げ前 提示画像2 武器投げ
6
- transform.localPosition;を利用してvoid Start()の時に初期の値を保存してそれを使って武器を飛ばしてプレイヤー追いついたときに値を書き換えましたが同じでした。
6
+
7
-
8
-
9
-
10
- 参考サイト:https://www.youtube.com/watch?v=QAFL1NXnqog&t=92s
11
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-04-12/635b4e25-0c90-4dd9-9456-b70abfb5135d.png)
7
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-04-13/69dbcd1d-76b5-4ab3-b57f-05bd3c435565.png)
8
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-04-13/66e162c9-bab8-4f92-acbd-5255c8fa6232.png)
9
+
10
+
12
- ##### Player_Sword
11
+ ##### Sword.cs
13
12
  ```cs
14
13
  using System.Collections;
15
14
  using System.Collections.Generic;
@@ -51,14 +50,18 @@
51
50
  // Start is called before the first frame update
52
51
  void Start()
53
52
  {
53
+
54
54
  parent = transform.parent;
55
+ //////////////////////////////////////////////////////////////////////////
56
+ position = transform.localPosition;
57
+ //////////////////////////////////////////////////////////////////////////
55
58
  animator = GetComponent<Animator>();
56
59
  }
57
60
 
58
61
  // Update is called once per frame
59
62
  void Update()
60
63
  {
61
- if ((animator.GetCurrentAnimatorStateInfo(0).IsName("attack") == false) && (animator.GetCurrentAnimatorStateInfo(0).IsName("Warp") == false) )
64
+ if ((animator.GetCurrentAnimatorStateInfo(0).IsName("attack") == false) && (animator.GetCurrentAnimatorStateInfo(0).IsName("sword_shoot") == false) )
62
65
  {
63
66
  if (Input.GetButtonDown("R1") == true)
64
67
  {
@@ -78,7 +81,7 @@
78
81
  isWarpShift = false;
79
82
  }
80
83
 
81
-
84
+ // 投げる
82
85
  if(isWarpShift == true)
83
86
  {
84
87
  if (isMoveCharacter == false)
@@ -113,7 +116,10 @@
113
116
  animator.speed = 1;
114
117
  animator.SetTrigger("WarpEnd");
115
118
  isMoveCharacter = false;
116
-
119
+ //////////////////////////////////////////////////////////////////////////
120
+ transform.localPosition = position;
121
+ transform.parent = parent;
122
+ //////////////////////////////////////////////////////////////////////////
117
123
  }
118
124
 
119
125
 
@@ -127,7 +133,7 @@
127
133
 
128
134
  ```
129
135
 
130
- ##### PlayerControl
136
+ ##### Contorl.cs
131
137
  ```cs
132
138
  using System.Collections;
133
139
  using System.Collections.Generic;
@@ -209,7 +215,11 @@
209
215
 
210
216
  if (velocity.magnitude > 0.3f)
211
217
  {
212
- transform.rotation = Quaternion.LookRotation(velocity, transform.up);
218
+ if ((weapon.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("attack") == false) && (weapon.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("sword_shoot") == false))
219
+ {
220
+ transform.rotation = Quaternion.LookRotation(velocity, transform.up);
221
+ }
222
+
213
223
  moveSpeed.x = (velocity * walkSpeed).x;
214
224
  moveSpeed.z = (velocity * walkSpeed).z;
215
225
  }
@@ -224,7 +234,7 @@
224
234
 
225
235
 
226
236
  // 攻撃していない時移動可能
227
- if( (weapon.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("attack") == false) && (weapon.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("Warp") == false) )
237
+ if( (weapon.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("attack") == false) && (weapon.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("sword_shoot") == false) )
228
238
  {
229
239
  Vector3 move = Vector3.Scale(moveSpeed, new Vector3(1, 0, 1));
230
240
  controller.Move(moveSpeed * Time.deltaTime); //移動
@@ -238,7 +248,6 @@
238
248
  {
239
249
  isJump = true;
240
250
  moveSpeed.y = 300;
241
-
242
251
  }
243
252
  else
244
253
  {
@@ -251,5 +260,3 @@
251
260
 
252
261
 
253
262
 
254
-
255
-

4

文章を修正

2022/04/13 05:43

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -3,6 +3,10 @@
3
3
 
4
4
  ##### 試したこと
5
5
  初期座標をvoid Start() で保存して変更された時にその値を使って書き換えるという処理を行いましたが上手く出来ませんでした。
6
+ transform.localPosition;を利用してvoid Start()の時に初期の値を保存してそれを使って武器を飛ばしてプレイヤー追いついたときに値を書き換えましたが同じでした。
7
+
8
+
9
+
6
10
  参考サイト:https://www.youtube.com/watch?v=QAFL1NXnqog&t=92s
7
11
  ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-04-12/635b4e25-0c90-4dd9-9456-b70abfb5135d.png)
8
12
  ##### Player_Sword

3

文章を修正しました。

2022/04/12 08:13

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,13 +1,8 @@
1
- 提示画像ですが青いオブジェクトが武器のスクリプトです。白いオブジェクトがプレイヤーです。提示サイトのように武器を飛ばした方向にプレイヤーが武器の方向に飛んでいくという処理を実装しているのですがプレイヤー武器の方向に飛んで行ったときに武器の位置が提示画像のような初期の座標に設定し直したのですがこれをするにはどうすればいいのでしょうか?
1
+ 提示画像ですが青いオブジェクトが武器のスクリプトです。白いオブジェクトがプレイヤースクリプトです。提示サイトのように武器を飛ばした方向にプレイヤーが武器の方向に飛んでいくという処理を実装しているのですがプレイヤー武器の方向に飛んで行ったときに武器の位置が提示画像のような初期の座標に設定し直したのですがこれをするにはどうすればいいのでしょうか?
2
2
  Player_Sword.cs下部の `WarpShiftEnd()`関数ですが最後はどのように`transform.position`を修正したらいいのでしょうか?
3
-
4
3
 
5
4
  ##### 試したこと
6
5
  初期座標をvoid Start() で保存して変更された時にその値を使って書き換えるという処理を行いましたが上手く出来ませんでした。
7
-
8
-
9
-
10
-
11
6
  参考サイト:https://www.youtube.com/watch?v=QAFL1NXnqog&t=92s
12
7
  ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-04-12/635b4e25-0c90-4dd9-9456-b70abfb5135d.png)
13
8
  ##### Player_Sword
@@ -36,33 +31,29 @@
36
31
  bool isMoveCharacter = false;
37
32
  float shiftDistance = 0;
38
33
 
39
- float shiftSpeed = 1000; //シフト速度
34
+ float shiftSpeed = 10; //シフト速度
40
35
  float shiftRange = 10; //ソフト距離
41
36
 
42
- Vector3 shootVector = new Vector3();
43
37
 
44
38
 
45
39
 
46
40
 
47
41
  public bool getIsWarpShift()
48
42
  {
49
- return isWarpShift;
43
+ return isMoveCharacter;
50
44
  }
51
45
 
52
46
 
53
47
  // Start is called before the first frame update
54
48
  void Start()
55
49
  {
56
- position = transform.position;
50
+ parent = transform.parent;
57
51
  animator = GetComponent<Animator>();
58
52
  }
59
53
 
60
54
  // Update is called once per frame
61
55
  void Update()
62
56
  {
63
- //shootVector = transform.parent.transform.forward; //移動方向
64
- shootVector = Vector3.forward; //移動方向
65
-
66
57
  if ((animator.GetCurrentAnimatorStateInfo(0).IsName("attack") == false) && (animator.GetCurrentAnimatorStateInfo(0).IsName("Warp") == false) )
67
58
  {
68
59
  if (Input.GetButtonDown("R1") == true)
@@ -77,26 +68,31 @@
77
68
  }
78
69
 
79
70
 
71
+
80
72
  if(animator.GetCurrentAnimatorStateInfo(0).IsName("idle") == true)
81
73
  {
82
74
  isWarpShift = false;
83
-
84
- }
75
+ }
85
-
86
-
76
+
77
+
87
78
  if(isWarpShift == true)
88
79
  {
89
-
90
- transform.Translate(shootVector * shiftSpeed * Time.deltaTime);
91
- shiftDistance += shiftSpeed * Time.deltaTime;
92
-
93
- if(shiftDistance > shiftRange)
80
+ if (isMoveCharacter == false)
94
- {
81
+ {
82
+ transform.Translate(Vector3.forward * 10 * Time.deltaTime);
83
+
84
+ shiftDistance += 10 * Time.deltaTime;
85
+ }
86
+
87
+ if (shiftDistance > 10)
88
+ {
89
+ transform.parent = null;
95
90
  isMoveCharacter = true;
96
91
  shiftDistance = 0;
97
92
  }
93
+
98
- }
94
+ }
99
-
95
+
100
96
  }
101
97
 
102
98
 
@@ -105,9 +101,6 @@
105
101
  {
106
102
  animator.speed = 0;
107
103
  isWarpShift = true;
108
-
109
-
110
-
111
104
  }
112
105
 
113
106
  /*########################################## アニメーション終了 ##########################################*/
@@ -115,11 +108,7 @@
115
108
  {
116
109
  animator.speed = 1;
117
110
  animator.SetTrigger("WarpEnd");
118
- position.y = 0;
119
111
  isMoveCharacter = false;
120
- isWarpShift = false;
121
-
122
- //transform.position = position;
123
112
 
124
113
  }
125
114
 
@@ -176,16 +165,17 @@
176
165
 
177
166
  if (weapon.GetComponent<Player_Sword>().getIsWarpShift() == true)
178
167
  {
179
-
180
168
  controller.Move(transform.forward * 10 * Time.deltaTime);
181
-
182
- shitRange += 10 * Time.deltaTime;
169
+ distance += 10 * Time.deltaTime;
183
- if (shitRange > weapon.GetComponent<Player_Sword>().getShiftRange())
170
+ if(distance > weapon.GetComponent<Player_Sword>().getShiftRange())
184
171
  {
185
172
  weapon.GetComponent<Player_Sword>().WarpShiftEnd();
173
+ distance = 0;
174
+
186
- }
175
+ }
176
+
187
- }
177
+ }
188
-
178
+
189
179
  }
190
180
 
191
181
 
@@ -232,12 +222,9 @@
232
222
  // 攻撃していない時移動可能
233
223
  if( (weapon.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("attack") == false) && (weapon.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("Warp") == false) )
234
224
  {
235
- //Debug.Log("ああ");
236
225
  Vector3 move = Vector3.Scale(moveSpeed, new Vector3(1, 0, 1));
237
226
  controller.Move(moveSpeed * Time.deltaTime); //移動
238
227
  }
239
-
240
-
241
228
  }
242
229
 
243
230
  /*########################################## ジャンプ ##########################################*/

2

文章を修正

2022/04/12 07:23

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- [Unity3D] 子のオブジェクト初期座標にする方法が知りたい。
1
+ [Unity3D] 子のオブジェクトの座標の変更方法で初期座標を一致させる方法が知りたい。
test CHANGED
@@ -1,4 +1,5 @@
1
1
  提示画像ですが青いオブジェクトが武器のスクリプトです。白いオブジェクトがプレイヤーです。提示サイトのように武器を飛ばした方向にプレイヤーが武器の方向に飛んでいくという処理を実装しているのですがプレイヤー武器の方向に飛んで行ったときに武器の位置が提示画像のような初期の座標に設定し直したのですがこれをするにはどうすればいいのでしょうか?
2
+ Player_Sword.cs下部の `WarpShiftEnd()`関数ですが最後はどのように`transform.position`を修正したらいいのでしょうか?
2
3
 
3
4
 
4
5
  ##### 試したこと

1

文章を修正

2022/04/12 07:06

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,9 +1,14 @@
1
- 提示コードですがソースファイルは以下の画像の青い武器のスクリプトです。提示コードコメント部ですが初期座標を保存して武器の座標が移動したとき最後に初期の座標に武器の座標設定しているのですが上手く提示画像の初期の座標に設定されず位置まいます。
1
+ 提示画像ですが青いオブジェクトが武器のスクリプトです。白いオブジェクトがプレイヤーです。提示サイトように武器を飛ばした方向プレイヤーが武器の方向に飛んでいくという処理実装しているのですがプレイヤー武器の方向に飛んで行ったときに武器の位置が提示画像のような初期の座標に設定し直したのですをするにはどうすればいいのでょうか?
2
+
3
+
4
+ ##### 試したこと
2
- どうやって子のオブジェクトが移動しても最座標に設定なおるのでしょうか?
5
+ 座標をvoid Start() で保存して変更された時その値を使って書き換えるという処理を行いまたが上手く出来までした。
6
+
7
+
8
+
9
+
3
-
10
+ 参考サイト:https://www.youtube.com/watch?v=QAFL1NXnqog&t=92s
4
-
5
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-04-12/068a311e-0ed3-4f6b-9d74-22744a657af5.png)
11
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-04-12/635b4e25-0c90-4dd9-9456-b70abfb5135d.png)
6
-
7
12
  ##### Player_Sword
8
13
  ```cs
9
14
  using System.Collections;
@@ -29,7 +34,10 @@
29
34
  Vector3 position;
30
35
  bool isMoveCharacter = false;
31
36
  float shiftDistance = 0;
37
+
38
+ float shiftSpeed = 1000; //シフト速度
32
- float shiftRange = 10;
39
+ float shiftRange = 10; //ソフト距離
40
+
33
41
  Vector3 shootVector = new Vector3();
34
42
 
35
43
 
@@ -44,10 +52,7 @@
44
52
  // Start is called before the first frame update
45
53
  void Start()
46
54
  {
47
- parent = transform.parent;
48
- /////////////////////////////////////
49
55
  position = transform.position;
50
- /////////////////////////////////////
51
56
  animator = GetComponent<Animator>();
52
57
  }
53
58
 
@@ -77,20 +82,18 @@
77
82
 
78
83
  }
79
84
 
80
-
85
+
81
86
  if(isWarpShift == true)
82
87
  {
83
- transform.parent = null;
88
+
84
- transform.Translate(shootVector * 1000 * Time.deltaTime);
89
+ transform.Translate(shootVector * shiftSpeed * Time.deltaTime);
85
- shiftDistance += 1000 * Time.deltaTime;
90
+ shiftDistance += shiftSpeed * Time.deltaTime;
86
-
91
+
87
- if(shiftDistance > 10)
92
+ if(shiftDistance > shiftRange)
88
93
  {
89
94
  isMoveCharacter = true;
95
+ shiftDistance = 0;
90
- }
96
+ }
91
-
92
-
93
-
94
97
  }
95
98
 
96
99
  }
@@ -99,8 +102,8 @@
99
102
  /*########################################## シフト移動開始 イベント ##########################################*/
100
103
  void OnWarp_Event()
101
104
  {
105
+ animator.speed = 0;
102
106
  isWarpShift = true;
103
- animator.speed = 0;
104
107
 
105
108
 
106
109
 
@@ -111,10 +114,12 @@
111
114
  {
112
115
  animator.speed = 1;
113
116
  animator.SetTrigger("WarpEnd");
117
+ position.y = 0;
114
- transform.parent = parent;
118
+ isMoveCharacter = false;
115
- ///////////////////////////////////////////////////
119
+ isWarpShift = false;
120
+
116
- transform.position = position;
121
+ //transform.position = position;
117
- ///////////////////////////////////////////////////
122
+
118
123
  }
119
124
 
120
125
 
@@ -123,15 +128,136 @@
123
128
  {
124
129
  return shiftRange;
125
130
  }
126
-
127
-
128
-
129
-
130
-
131
-
132
131
  }
133
-
134
-
135
-
132
+ }
133
+
134
+ ```
135
+
136
+ ##### PlayerControl
137
+ ```cs
138
+ using System.Collections;
139
+ using System.Collections.Generic;
140
+ using UnityEngine;
141
+
142
+ namespace Player
143
+ {
144
+
145
+ /*
146
+ * #################################################
147
+ * プレイヤーコントロール
148
+ * #################################################
149
+ */
150
+
151
+ public class Player_Control : MonoBehaviour
152
+ {
153
+
154
+ [SerializeField] const float walkSpeed = 10; //移動速度
155
+ [SerializeField] const float fallSpeed = 15; //落下速度
156
+ [SerializeField] GameObject weapon; //武器
157
+
158
+ Vector3 moveSpeed;
159
+ bool isJump = false;
160
+ CharacterController controller;
161
+ Animator animator;
162
+ float distance = 0;
163
+
164
+
165
+ void Start()
166
+ {
167
+ controller = GetComponent<CharacterController>();
168
+ animator = GetComponent<Animator>();
169
+ }
170
+
171
+ void Update()
172
+ {
173
+ Move();
174
+
175
+
176
+ if (weapon.GetComponent<Player_Sword>().getIsWarpShift() == true)
177
+ {
178
+
179
+ controller.Move(transform.forward * 10 * Time.deltaTime);
180
+
181
+ shitRange += 10 * Time.deltaTime;
182
+ if (shitRange > weapon.GetComponent<Player_Sword>().getShiftRange())
183
+ {
184
+ weapon.GetComponent<Player_Sword>().WarpShiftEnd();
185
+ }
186
+ }
187
+
188
+ }
189
+
190
+
191
+ void FixedUpdate()
192
+ {
193
+
194
+ }
195
+
196
+ float shitRange = 0;
197
+
198
+ /*########################################## Late Update ##########################################*/
199
+ void LateUpdate()
200
+ {
201
+
202
+ }
203
+
204
+
205
+
206
+ /*########################################## 移動 ##########################################*/
207
+ void Move()
208
+ {
209
+ float inputHorizontal = Input.GetAxis("Left_Horizontal");
210
+ float inputVertical = Input.GetAxis("Left_Vertical");
211
+
212
+ Quaternion horizontalRotation = Quaternion.AngleAxis(Camera.main.transform.eulerAngles.y,Vector3.up);
213
+ Vector3 velocity = horizontalRotation * new Vector3(inputHorizontal, 0, inputVertical * -1).normalized;
214
+
215
+ if (velocity.magnitude > 0.3f)
216
+ {
217
+ transform.rotation = Quaternion.LookRotation(velocity, transform.up);
218
+ moveSpeed.x = (velocity * walkSpeed).x;
219
+ moveSpeed.z = (velocity * walkSpeed).z;
220
+ }
221
+ else
222
+ {
223
+ moveSpeed.x = 0;
224
+ moveSpeed.z = 0;
225
+ }
226
+
227
+
228
+ Jump();
229
+
230
+
231
+ // 攻撃していない時移動可能
232
+ if( (weapon.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("attack") == false) && (weapon.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("Warp") == false) )
233
+ {
234
+ //Debug.Log("ああ");
235
+ Vector3 move = Vector3.Scale(moveSpeed, new Vector3(1, 0, 1));
236
+ controller.Move(moveSpeed * Time.deltaTime); //移動
237
+ }
238
+
239
+
240
+ }
241
+
242
+ /*########################################## ジャンプ ##########################################*/
243
+ void Jump()
244
+ {
245
+ if( (Input.GetButtonDown("Cross") == true) && (controller.isGrounded == true) )
246
+ {
247
+ isJump = true;
248
+ moveSpeed.y = 300;
249
+
250
+ }
251
+ else
252
+ {
253
+ moveSpeed.y = -20;
254
+ }
255
+ }
256
+ }
136
257
  }
137
258
  ```
259
+
260
+
261
+
262
+
263
+