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

質問編集履歴

5

カyラクターアタッチスクリプト追加。

2018/04/29 18:23

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -11,19 +11,18 @@
11
11
  ![イメージ説明](05ca5e5a3bade47e5dca32003d6c10eb.jpeg)
12
12
 
13
13
  このポーズをしている状態で、コライダーを設置する方法はありますか?
14
- もしくは剣だけにコライダー設置したいのですが、何か良い方法はありますか?
14
+ もしくは何か良い方法はありますか?
15
15
  ご教示のほどよろしくお願いします。
16
16
 
17
17
  ### 追記①
18
18
  アセットは以下です。
19
- 現在ダウンロードできないかもしれません。
19
+ 現在ダウンロードできないかもしれません。
20
20
  https://www.assetstore.unity3d.com/jp/#!/content/48768
21
21
 
22
22
  ### 追記②
23
23
 
24
24
  spine_2を調べたら、swordの下にswordcolliderというコライダーらしきゲームオブジェクトを見つけました。
25
25
  しかし、インスペクタを表示してみると、コライダーがアタッチされていません。
26
- もうちょっと調べてみようと思います。
27
26
 
28
27
  ![イメージ説明](3da95404018f7262b304005fb9467b09.jpeg)
29
28
 
@@ -39,12 +38,10 @@
39
38
  public Transform spawnobject;
40
39
  public float radius = 3.0f;
41
40
  public float power = 100.0f;
42
- // Use this for initialization
43
41
  void Start () {
44
42
 
45
43
  }
46
44
 
47
- // Update is called once per frame
48
45
  void Update ()
49
46
  {
50
47
  if (hitpoints <= 0)
@@ -73,4 +70,330 @@
73
70
  }
74
71
  }
75
72
 
73
+ ```
74
+
75
+ ### 追記④
76
+
77
+ キャラクターにアタッチされているスクリプト。
78
+ ```
79
+ using UnityEngine;
80
+ using System.Collections;
81
+ using UnityEngine.UI;
82
+ public class simplecontroller : MonoBehaviour {
83
+
84
+
85
+ public Transform mycamera;
86
+ private Transform reference;
87
+
88
+ public float jumpHeight = 2.0f;
89
+ public float jumpinterval = 1.5f;
90
+ private float nextjump = 1.2f;
91
+ private float maxhitpoints = 1000f;
92
+ private float hitpoints = 1000f;
93
+ public float regen = 100f;
94
+
95
+ public AudioClip[] hurtsounds;
96
+ public AudioClip[] jumpsounds;
97
+ public AudioClip[] attacksounds;
98
+
99
+
100
+ public float gravity = 20.0f;
101
+ public float rotatespeed = 4.0f;
102
+ private float wantedrotatespeed;
103
+ public float normalspeed = 4.0f;
104
+ public float runspeed = 8.0f;
105
+
106
+ public float dampTime = 2.0f;
107
+
108
+ private bool isattack;
109
+
110
+
111
+ public Transform target;
112
+ private float moveAmount;
113
+ public float smoothSpeed = 2.0f;
114
+
115
+ private Vector3 forward = Vector3.forward;
116
+ private Vector3 moveDirection = Vector3.zero;
117
+ private Vector3 right;
118
+
119
+ private float movespeed;
120
+ public Vector3 localvelocity;
121
+
122
+ public bool running = false;
123
+ public bool canrun = true;
124
+
125
+ AudioSource myaudiosource;
126
+
127
+ Vector3 targetDirection = Vector3.zero;
128
+
129
+ Vector3 targetVelocity;
130
+ public GameObject impactprefab;
131
+ public LayerMask mask;
132
+ public float rayfiredelay = 0.35f;
133
+ public float force = 1000f;
134
+ public float damage = 50f;
135
+ public Transform Deadprefab;
136
+ Animator animator;
137
+ CharacterController controller;
138
+ Quaternion oldrotation;
139
+ Quaternion currentrotation;
140
+
141
+ public int attackanimcount;
142
+ private int prevrandom = 0;
143
+ private float randomattackfloat;
144
+ public Transform shield;
145
+ public Transform weapon;
146
+ public Transform lefthandpos;
147
+ public Transform righthandpos;
148
+ public Transform chestposshield;
149
+ public Transform chestposweapon;
150
+ public AudioClip[] switchsounds;
151
+ public AudioClip[] wooshsounds;
152
+
153
+ private float addfloat = 0f;
154
+ private bool fightmodus = false;
155
+ private bool didselect;
156
+ private bool canattack= false;
157
+
158
+ void Awake ()
159
+ {
160
+
161
+ myaudiosource = GetComponent<AudioSource>();
162
+ myaudiosource.loop = false;
163
+ reference = new GameObject().transform;
164
+ animator = GetComponent<Animator>();
165
+ controller = GetComponent<CharacterController>();
166
+
167
+ }
168
+
169
+
170
+ void Start ()
171
+ {
172
+ Cursor.lockState = CursorLockMode.Locked;
173
+ }
174
+
175
+ void Update ()
176
+ {
177
+
178
+ reference.eulerAngles = new Vector3(0, mycamera.eulerAngles.y, 0);
179
+ forward = reference.forward;
180
+ right = new Vector3(forward.z, 0, -forward.x);
181
+ float hor = Input.GetAxis("Horizontal") * 5f * Time.deltaTime;
182
+ float ver = Input.GetAxis("Vertical") * 5f * Time.deltaTime;
183
+
184
+
185
+
186
+
187
+ Vector3 velocity = controller.velocity;
188
+ localvelocity = transform.InverseTransformDirection(velocity);
189
+
190
+ bool ismovingforward =localvelocity.z > .5f;
191
+
192
+ targetDirection = (hor * right) + (ver * forward);
193
+ targetDirection = targetDirection.normalized;
194
+ targetVelocity = targetDirection;
195
+
196
+
197
+
198
+ if (fightmodus)
199
+ {
200
+
201
+ if (targetDirection == Vector3.zero) {
202
+ Vector3 localTarget = transform.InverseTransformPoint (target.position);
203
+ addfloat = (Mathf.Atan2 (localTarget.x, localTarget.z));
204
+ }
205
+ else
206
+ {
207
+
208
+ addfloat = 0f;
209
+ }
210
+ if (running)
211
+ {
212
+ if (targetDirection != Vector3.zero)
213
+ {(省略)
214
+ }
215
+ }
216
+ else
217
+ {(省略)
218
+ }
219
+ }
220
+ else
221
+ {
222
+ addfloat = 0f;
223
+ lookweight = 0.1f;
224
+ if (targetDirection != Vector3.zero)
225
+ {
226
+ var lookrotation2 = Quaternion.LookRotation(targetDirection,Vector3.up);
227
+ lookrotation2.x = 0;
228
+ lookrotation2.z = 0;
229
+ transform.rotation = Quaternion.Lerp(transform.rotation,lookrotation2,Time.deltaTime * wantedrotatespeed);
230
+ }
231
+
232
+ }
233
+
234
+
235
+ if (Input.GetButtonDown("Fire3") && Time.time > nextselect && canequip)
236
+ {
237
+ StartCoroutine (weaponselect ());
238
+ nextselect = Time.time + 2f;
239
+ }
240
+
241
+ if (canattack && controller.isGrounded)
242
+ {
243
+ bool attackState = animator.GetCurrentAnimatorStateInfo(1).IsName("attacks");
244
+ if (attackState) {
245
+ canjump = false;
246
+ //animator.applyRootMotion = true;
247
+ }
248
+ else
249
+ {
250
+ canjump = true;
251
+ //animator.applyRootMotion = false;
252
+ }
253
+ if (Input.GetButtonDown("Fire1"))
254
+ {
255
+ if (!attackState)
256
+ {
257
+ StartCoroutine(raycastattack());
258
+ int randomattack = randomIntExcept (0, attackanimcount, prevrandom);
259
+
260
+ randomattackfloat = randomattack * 1.0f;
261
+ animator.SetBool ("attack", true);
262
+ animator.SetFloat ("random", randomattackfloat);
263
+
264
+ int n = Random.Range (1, attacksounds.Length);
265
+ myaudiosource.clip = attacksounds [n];
266
+ myaudiosource.pitch = 0.9f + 0.1f * Random.value;
267
+ myaudiosource.Play ();
268
+ attacksounds [n] = attacksounds [0];
269
+ attacksounds [0] = myaudiosource.clip;
270
+ prevrandom = randomattack;
271
+ }
272
+ }
273
+ else
274
+ {
275
+ animator.SetBool("attack",false);
276
+ }
277
+ }
278
+
279
+ if (controller.isGrounded)
280
+ {
281
+ canequip = true;
282
+ lastspeed = Mathf.Lerp(lastspeed,speed,Time.deltaTime * 3f);
283
+ if (Input.GetButton ("Jump") && canjump && Time.time > nextjump)
284
+ { (省略)
285
+ }
286
+ }
287
+
288
+ else
289
+ {
290
+ canequip = false;
291
+ moveDirection.y -= (gravity) * Time.deltaTime;
292
+ nextjump = Time.time + jumpinterval;
293
+ animator.SetBool ("jump",false);
294
+ }
295
+
296
+ if (Input.GetButton ("Fire2") && canrun && ismovingforward)
297
+ {
298
+ speed = runspeed;
299
+ running = true;
300
+ wantedrotatespeed = rotatespeed;
301
+ }
302
+ else
303
+ {
304
+ speed = normalspeed;
305
+ running = false;
306
+ wantedrotatespeed = rotatespeed;
307
+
308
+ }
309
+ targetVelocity *= lastspeed;
310
+ moveDirection.z = targetVelocity.z;
311
+ moveDirection.x = targetVelocity.x;
312
+
313
+ animator.SetFloat("hor",((localvelocity.x/normalspeed) + addfloat), dampTime , 0.8f);
314
+ animator.SetFloat("ver",((localvelocity.z/normalspeed)), dampTime , 0.8f);
315
+ if (hitpoints <= 0)
316
+ {
317
+ //die
318
+ Instantiate(Deadprefab, transform.position, transform.rotation);
319
+ Destroy(gameObject);
320
+ }
321
+
322
+ animator.SetBool("grounded",controller.isGrounded);
323
+ controller.Move (moveDirection * Time.deltaTime);
324
+
325
+ if (hitpoints < maxhitpoints)
326
+ hitpoints += regen * Time.deltaTime;
327
+
328
+ }
329
+ void Damage (float damage)
330
+ {
331
+ if (!myaudiosource.isPlaying && hitpoints >= 0)
332
+ {
333
+ int n = Random.Range(1,hurtsounds.Length);
334
+ myaudiosource.clip = hurtsounds[n];
335
+ myaudiosource.pitch = 0.9f + 0.1f *Random.value;
336
+ myaudiosource.Play();
337
+ hurtsounds[n] = hurtsounds[0];
338
+ hurtsounds[0] = myaudiosource.clip;
339
+ }
340
+ //damaged = true;
341
+ //myAudioSource.PlayOneShot(hurtsound);
342
+ hitpoints = hitpoints - damage;
343
+ }
344
+
345
+ IEnumerator equip()
346
+ {
347
+      (省略)
348
+ }
349
+ IEnumerator raycastattack()
350
+ {
351
+ yield return new WaitForSeconds(rayfiredelay);
352
+ Vector3 fwrd = mycamera.transform.forward;
353
+
354
+ Vector3 camUp = mycamera.transform.up;
355
+ Vector3 camRight = mycamera.transform.right;
356
+
357
+ Vector3 wantedvector = fwrd;
358
+ wantedvector += Random.Range( -.1f, .1f ) * camUp + Random.Range( -.1f, .1f ) * camRight;
359
+
360
+ Ray ray = new Ray (transform.position + Vector3.up,wantedvector);
361
+ RaycastHit hit = new RaycastHit();
362
+
363
+ if (Physics.Raycast(ray,out hit, 3f,mask))
364
+ {
365
+
366
+ if(hit.rigidbody) hit.rigidbody.AddForceAtPosition (force * fwrd , hit.point);
367
+ hit.transform.SendMessageUpwards ("Damage",damage, SendMessageOptions.DontRequireReceiver);
368
+ GameObject decal;
369
+
370
+ decal = Instantiate(impactprefab, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)) as GameObject ;
371
+ decal.transform.localRotation = decal.transform.localRotation * Quaternion.Euler(0f,Random.Range(-90f,90f),0f);
372
+ }
373
+
374
+
375
+ }
376
+ IEnumerator holster()
377
+ {
378
+ yield return new WaitForSeconds(.3f);
379
+ shield.parent = chestposshield;
380
+ shield.position = chestposshield.position;
381
+ shield.rotation = chestposshield.rotation;
382
+
383
+ int n = Random.Range(1,switchsounds.Length);
384
+ myaudiosource.clip = switchsounds[n];
385
+ myaudiosource.pitch = 0.9f + 0.1f *Random.value;
386
+ myaudiosource.Play();
387
+ switchsounds[n] = switchsounds[0];
388
+ switchsounds[0] = myaudiosource.clip;
389
+
390
+
391
+ weapon.parent = chestposweapon;
392
+ weapon.position = chestposweapon.position;
393
+ weapon.rotation = chestposweapon.rotation;
394
+
395
+
396
+ }
397
+
398
+ }
76
399
  ```

4

樽アセットスクリプト

2018/04/29 18:23

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -25,4 +25,52 @@
25
25
  しかし、インスペクタを表示してみると、コライダーがアタッチされていません。
26
26
  もうちょっと調べてみようと思います。
27
27
 
28
- ![イメージ説明](3da95404018f7262b304005fb9467b09.jpeg)
28
+ ![イメージ説明](3da95404018f7262b304005fb9467b09.jpeg)
29
+
30
+ ### 追記③
31
+
32
+ 樽アセットのスクリプト。
33
+ ```C#
34
+ using UnityEngine;
35
+ using System.Collections;
36
+
37
+ public class crate : MonoBehaviour {
38
+ public float hitpoints = 100f;
39
+ public Transform spawnobject;
40
+ public float radius = 3.0f;
41
+ public float power = 100.0f;
42
+ // Use this for initialization
43
+ void Start () {
44
+
45
+ }
46
+
47
+ // Update is called once per frame
48
+ void Update ()
49
+ {
50
+ if (hitpoints <= 0)
51
+ {
52
+ Instantiate(spawnobject, transform.position, transform.rotation);
53
+ Vector3 explosionPos = transform.position;
54
+ Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);
55
+ foreach (Collider hit in colliders)
56
+ {
57
+ if (hit.GetComponent<Rigidbody>() != null)
58
+ {
59
+ Rigidbody rb = hit.GetComponent<Rigidbody>();
60
+ rb.AddExplosionForce(power, explosionPos, radius, 3.0f);
61
+
62
+ }
63
+ }
64
+ Destroy (gameObject);
65
+ }
66
+
67
+ }
68
+ void Damage (float damage)
69
+ {
70
+
71
+
72
+ hitpoints = hitpoints - damage;
73
+ }
74
+ }
75
+
76
+ ```

3

修正

2018/04/27 17:35

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
File without changes

2

修正

2018/04/27 14:33

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -14,8 +14,13 @@
14
14
  もしくは剣だけにコライダー設置したいのですが、何か良い方法はありますか?
15
15
  ご教示のほどよろしくお願いします。
16
16
 
17
- ### 追記
17
+ ### 追記
18
+ アセットは以下です。
19
+ 現在、ダウンロードできないかもしれません。
20
+ https://www.assetstore.unity3d.com/jp/#!/content/48768
18
21
 
22
+ ### 追記②
23
+
19
24
  spine_2を調べたら、swordの下にswordcolliderというコライダーらしきゲームオブジェクトを見つけました。
20
25
  しかし、インスペクタを表示してみると、コライダーがアタッチされていません。
21
26
  もうちょっと調べてみようと思います。

1

修正

2018/04/27 14:32

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -12,4 +12,12 @@
12
12
 
13
13
  このポーズをしている状態で、コライダーを設置する方法はありますか?
14
14
  もしくは剣だけにコライダー設置したいのですが、何か良い方法はありますか?
15
- ご教示のほどよろしくお願いします。
15
+ ご教示のほどよろしくお願いします。
16
+
17
+ ### 追記
18
+
19
+ spine_2を調べたら、swordの下にswordcolliderというコライダーらしきゲームオブジェクトを見つけました。
20
+ しかし、インスペクタを表示してみると、コライダーがアタッチされていません。
21
+ もうちょっと調べてみようと思います。
22
+
23
+ ![イメージ説明](3da95404018f7262b304005fb9467b09.jpeg)