質問編集履歴

1

回答を元にスクリプトを修正してみました。

2018/11/09 15:00

投稿

nabesi
nabesi

スコア13

test CHANGED
File without changes
test CHANGED
@@ -87,3 +87,115 @@
87
87
  }
88
88
 
89
89
  ```
90
+
91
+ 追記
92
+
93
+ ```ここに言語を入力
94
+
95
+ using System;
96
+
97
+ using UnityEngine;
98
+
99
+
100
+
101
+ //Playerへアタッチするスクリプト
102
+
103
+ //MainCameraはPlayerの子要素
104
+
105
+
106
+
107
+ public class GazeController : MonoBehaviour
108
+
109
+ {
110
+
111
+ new Rigidbody rigidbody; //リジッドボディーを使用
112
+
113
+ public new GameObject camera; //カメラ
114
+
115
+
116
+
117
+ //初期化
118
+
119
+ void Start()
120
+
121
+ {
122
+
123
+ this.rigidbody = GetComponent<Rigidbody>();
124
+
125
+ this.camera = GameObject.Find("Main Camera");
126
+
127
+ }
128
+
129
+
130
+
131
+ //定期的に呼ばれる
132
+
133
+ void FixedUpdate()
134
+
135
+ {
136
+
137
+ PlayerController();
138
+
139
+ }
140
+
141
+
142
+
143
+ private void PlayerController()
144
+
145
+ { //cameraのVector3をlocalupへ取得
146
+
147
+ var localup = camera.transform.rotation * Vector3.up;
148
+
149
+
150
+
151
+ var frontangle = Mathf.Atan2(localup.y, localup.z) * Mathf.Rad2Deg; //上下角度
152
+
153
+ var sideangle = Mathf.Atan2(localup.y, localup.x) * Mathf.Rad2Deg; //左右角度
154
+
155
+
156
+
157
+ //取得した数値が360になるように270を足す( Debug.Logで数値を確認)
158
+
159
+ if (frontangle < 180) frontangle += 270;
160
+
161
+ if (sideangle < 180) sideangle += 270;
162
+
163
+
164
+
165
+ // 最小値を返す関数、最初は(frontangle - 360f)=0からのスタートとする
166
+
167
+ float FB = (Math.Min((frontangle - 360f), 10f) / 10f) * 5f ; //上下角度
168
+
169
+ float LR = (Math.Min((360f - sideangle), 15f) / 15f) * 1f; //左右角度
170
+
171
+ Debug.Log("FB:" + FB); //Angle数値の確認用
172
+
173
+ Debug.Log("LR:" + LR);
174
+
175
+
176
+
177
+
178
+
179
+ //Playerの向きと加速度の指定
180
+
181
+ //前後
182
+
183
+ this.rigidbody.velocity = this.gameObject.transform.rotation * new Vector3(0, 0, FB);
184
+
185
+
186
+
187
+ //左右
188
+
189
+ if (FB != 0f)
190
+
191
+ {
192
+
193
+ this.gameObject.transform.Rotate(0, (FB > 0) ? LR : -LR, 0);
194
+
195
+ }
196
+
197
+ }
198
+
199
+ }
200
+
201
+ ```