質問編集履歴

5

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

2018/04/29 18:23

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -24,7 +24,7 @@
24
24
 
25
25
  このポーズをしている状態で、コライダーを設置する方法はありますか?
26
26
 
27
- もしくは剣だけにコライダー設置したいのですが、何か良い方法はありますか?
27
+ もしくは何か良い方法はありますか?
28
28
 
29
29
  ご教示のほどよろしくお願いします。
30
30
 
@@ -34,7 +34,7 @@
34
34
 
35
35
  アセットは以下です。
36
36
 
37
- 現在ダウンロードできないかもしれません。
37
+ 現在ダウンロードできないかもしれません。
38
38
 
39
39
  https://www.assetstore.unity3d.com/jp/#!/content/48768
40
40
 
@@ -48,8 +48,6 @@
48
48
 
49
49
  しかし、インスペクタを表示してみると、コライダーがアタッチされていません。
50
50
 
51
- もうちょっと調べてみようと思います。
52
-
53
51
 
54
52
 
55
53
  ![イメージ説明](3da95404018f7262b304005fb9467b09.jpeg)
@@ -80,8 +78,6 @@
80
78
 
81
79
  public float power = 100.0f;
82
80
 
83
- // Use this for initialization
84
-
85
81
  void Start () {
86
82
 
87
83
 
@@ -90,8 +86,6 @@
90
86
 
91
87
 
92
88
 
93
- // Update is called once per frame
94
-
95
89
  void Update ()
96
90
 
97
91
  {
@@ -149,3 +143,655 @@
149
143
 
150
144
 
151
145
  ```
146
+
147
+
148
+
149
+ ### 追記④
150
+
151
+
152
+
153
+ キャラクターにアタッチされているスクリプト。
154
+
155
+ ```
156
+
157
+ using UnityEngine;
158
+
159
+ using System.Collections;
160
+
161
+ using UnityEngine.UI;
162
+
163
+ public class simplecontroller : MonoBehaviour {
164
+
165
+
166
+
167
+
168
+
169
+ public Transform mycamera;
170
+
171
+ private Transform reference;
172
+
173
+
174
+
175
+ public float jumpHeight = 2.0f;
176
+
177
+ public float jumpinterval = 1.5f;
178
+
179
+ private float nextjump = 1.2f;
180
+
181
+ private float maxhitpoints = 1000f;
182
+
183
+ private float hitpoints = 1000f;
184
+
185
+ public float regen = 100f;
186
+
187
+
188
+
189
+ public AudioClip[] hurtsounds;
190
+
191
+ public AudioClip[] jumpsounds;
192
+
193
+ public AudioClip[] attacksounds;
194
+
195
+
196
+
197
+
198
+
199
+ public float gravity = 20.0f;
200
+
201
+ public float rotatespeed = 4.0f;
202
+
203
+ private float wantedrotatespeed;
204
+
205
+ public float normalspeed = 4.0f;
206
+
207
+ public float runspeed = 8.0f;
208
+
209
+
210
+
211
+ public float dampTime = 2.0f;
212
+
213
+
214
+
215
+ private bool isattack;
216
+
217
+
218
+
219
+
220
+
221
+ public Transform target;
222
+
223
+ private float moveAmount;
224
+
225
+ public float smoothSpeed = 2.0f;
226
+
227
+
228
+
229
+ private Vector3 forward = Vector3.forward;
230
+
231
+ private Vector3 moveDirection = Vector3.zero;
232
+
233
+ private Vector3 right;
234
+
235
+
236
+
237
+ private float movespeed;
238
+
239
+ public Vector3 localvelocity;
240
+
241
+
242
+
243
+ public bool running = false;
244
+
245
+ public bool canrun = true;
246
+
247
+
248
+
249
+ AudioSource myaudiosource;
250
+
251
+
252
+
253
+ Vector3 targetDirection = Vector3.zero;
254
+
255
+
256
+
257
+ Vector3 targetVelocity;
258
+
259
+ public GameObject impactprefab;
260
+
261
+ public LayerMask mask;
262
+
263
+ public float rayfiredelay = 0.35f;
264
+
265
+ public float force = 1000f;
266
+
267
+ public float damage = 50f;
268
+
269
+ public Transform Deadprefab;
270
+
271
+ Animator animator;
272
+
273
+ CharacterController controller;
274
+
275
+ Quaternion oldrotation;
276
+
277
+ Quaternion currentrotation;
278
+
279
+
280
+
281
+ public int attackanimcount;
282
+
283
+ private int prevrandom = 0;
284
+
285
+ private float randomattackfloat;
286
+
287
+ public Transform shield;
288
+
289
+ public Transform weapon;
290
+
291
+ public Transform lefthandpos;
292
+
293
+ public Transform righthandpos;
294
+
295
+ public Transform chestposshield;
296
+
297
+ public Transform chestposweapon;
298
+
299
+ public AudioClip[] switchsounds;
300
+
301
+ public AudioClip[] wooshsounds;
302
+
303
+
304
+
305
+ private float addfloat = 0f;
306
+
307
+ private bool fightmodus = false;
308
+
309
+ private bool didselect;
310
+
311
+ private bool canattack= false;
312
+
313
+
314
+
315
+ void Awake ()
316
+
317
+ {
318
+
319
+
320
+
321
+ myaudiosource = GetComponent<AudioSource>();
322
+
323
+ myaudiosource.loop = false;
324
+
325
+ reference = new GameObject().transform;
326
+
327
+ animator = GetComponent<Animator>();
328
+
329
+ controller = GetComponent<CharacterController>();
330
+
331
+
332
+
333
+ }
334
+
335
+
336
+
337
+
338
+
339
+ void Start ()
340
+
341
+ {
342
+
343
+ Cursor.lockState = CursorLockMode.Locked;
344
+
345
+ }
346
+
347
+
348
+
349
+ void Update ()
350
+
351
+ {
352
+
353
+
354
+
355
+ reference.eulerAngles = new Vector3(0, mycamera.eulerAngles.y, 0);
356
+
357
+ forward = reference.forward;
358
+
359
+ right = new Vector3(forward.z, 0, -forward.x);
360
+
361
+ float hor = Input.GetAxis("Horizontal") * 5f * Time.deltaTime;
362
+
363
+ float ver = Input.GetAxis("Vertical") * 5f * Time.deltaTime;
364
+
365
+
366
+
367
+
368
+
369
+
370
+
371
+
372
+
373
+ Vector3 velocity = controller.velocity;
374
+
375
+ localvelocity = transform.InverseTransformDirection(velocity);
376
+
377
+
378
+
379
+ bool ismovingforward =localvelocity.z > .5f;
380
+
381
+
382
+
383
+ targetDirection = (hor * right) + (ver * forward);
384
+
385
+ targetDirection = targetDirection.normalized;
386
+
387
+ targetVelocity = targetDirection;
388
+
389
+
390
+
391
+
392
+
393
+
394
+
395
+ if (fightmodus)
396
+
397
+ {
398
+
399
+
400
+
401
+ if (targetDirection == Vector3.zero) {
402
+
403
+ Vector3 localTarget = transform.InverseTransformPoint (target.position);
404
+
405
+ addfloat = (Mathf.Atan2 (localTarget.x, localTarget.z));
406
+
407
+ }
408
+
409
+ else
410
+
411
+ {
412
+
413
+
414
+
415
+ addfloat = 0f;
416
+
417
+ }
418
+
419
+ if (running)
420
+
421
+ {
422
+
423
+ if (targetDirection != Vector3.zero)
424
+
425
+ {(省略)
426
+
427
+ }
428
+
429
+ }
430
+
431
+ else
432
+
433
+ {(省略)
434
+
435
+ }
436
+
437
+ }
438
+
439
+ else
440
+
441
+ {
442
+
443
+ addfloat = 0f;
444
+
445
+ lookweight = 0.1f;
446
+
447
+ if (targetDirection != Vector3.zero)
448
+
449
+ {
450
+
451
+ var lookrotation2 = Quaternion.LookRotation(targetDirection,Vector3.up);
452
+
453
+ lookrotation2.x = 0;
454
+
455
+ lookrotation2.z = 0;
456
+
457
+ transform.rotation = Quaternion.Lerp(transform.rotation,lookrotation2,Time.deltaTime * wantedrotatespeed);
458
+
459
+ }
460
+
461
+
462
+
463
+ }
464
+
465
+
466
+
467
+
468
+
469
+ if (Input.GetButtonDown("Fire3") && Time.time > nextselect && canequip)
470
+
471
+ {
472
+
473
+ StartCoroutine (weaponselect ());
474
+
475
+ nextselect = Time.time + 2f;
476
+
477
+ }
478
+
479
+
480
+
481
+ if (canattack && controller.isGrounded)
482
+
483
+ {
484
+
485
+ bool attackState = animator.GetCurrentAnimatorStateInfo(1).IsName("attacks");
486
+
487
+ if (attackState) {
488
+
489
+ canjump = false;
490
+
491
+ //animator.applyRootMotion = true;
492
+
493
+ }
494
+
495
+ else
496
+
497
+ {
498
+
499
+ canjump = true;
500
+
501
+ //animator.applyRootMotion = false;
502
+
503
+ }
504
+
505
+ if (Input.GetButtonDown("Fire1"))
506
+
507
+ {
508
+
509
+ if (!attackState)
510
+
511
+ {
512
+
513
+ StartCoroutine(raycastattack());
514
+
515
+ int randomattack = randomIntExcept (0, attackanimcount, prevrandom);
516
+
517
+
518
+
519
+ randomattackfloat = randomattack * 1.0f;
520
+
521
+ animator.SetBool ("attack", true);
522
+
523
+ animator.SetFloat ("random", randomattackfloat);
524
+
525
+
526
+
527
+ int n = Random.Range (1, attacksounds.Length);
528
+
529
+ myaudiosource.clip = attacksounds [n];
530
+
531
+ myaudiosource.pitch = 0.9f + 0.1f * Random.value;
532
+
533
+ myaudiosource.Play ();
534
+
535
+ attacksounds [n] = attacksounds [0];
536
+
537
+ attacksounds [0] = myaudiosource.clip;
538
+
539
+ prevrandom = randomattack;
540
+
541
+ }
542
+
543
+ }
544
+
545
+ else
546
+
547
+ {
548
+
549
+ animator.SetBool("attack",false);
550
+
551
+ }
552
+
553
+ }
554
+
555
+
556
+
557
+ if (controller.isGrounded)
558
+
559
+ {
560
+
561
+ canequip = true;
562
+
563
+ lastspeed = Mathf.Lerp(lastspeed,speed,Time.deltaTime * 3f);
564
+
565
+ if (Input.GetButton ("Jump") && canjump && Time.time > nextjump)
566
+
567
+ { (省略)
568
+
569
+ }
570
+
571
+ }
572
+
573
+
574
+
575
+ else
576
+
577
+ {
578
+
579
+ canequip = false;
580
+
581
+ moveDirection.y -= (gravity) * Time.deltaTime;
582
+
583
+ nextjump = Time.time + jumpinterval;
584
+
585
+ animator.SetBool ("jump",false);
586
+
587
+ }
588
+
589
+
590
+
591
+ if (Input.GetButton ("Fire2") && canrun && ismovingforward)
592
+
593
+ {
594
+
595
+ speed = runspeed;
596
+
597
+ running = true;
598
+
599
+ wantedrotatespeed = rotatespeed;
600
+
601
+ }
602
+
603
+ else
604
+
605
+ {
606
+
607
+ speed = normalspeed;
608
+
609
+ running = false;
610
+
611
+ wantedrotatespeed = rotatespeed;
612
+
613
+
614
+
615
+ }
616
+
617
+ targetVelocity *= lastspeed;
618
+
619
+ moveDirection.z = targetVelocity.z;
620
+
621
+ moveDirection.x = targetVelocity.x;
622
+
623
+
624
+
625
+ animator.SetFloat("hor",((localvelocity.x/normalspeed) + addfloat), dampTime , 0.8f);
626
+
627
+ animator.SetFloat("ver",((localvelocity.z/normalspeed)), dampTime , 0.8f);
628
+
629
+ if (hitpoints <= 0)
630
+
631
+ {
632
+
633
+ //die
634
+
635
+ Instantiate(Deadprefab, transform.position, transform.rotation);
636
+
637
+ Destroy(gameObject);
638
+
639
+ }
640
+
641
+
642
+
643
+ animator.SetBool("grounded",controller.isGrounded);
644
+
645
+ controller.Move (moveDirection * Time.deltaTime);
646
+
647
+
648
+
649
+ if (hitpoints < maxhitpoints)
650
+
651
+ hitpoints += regen * Time.deltaTime;
652
+
653
+
654
+
655
+ }
656
+
657
+ void Damage (float damage)
658
+
659
+ {
660
+
661
+ if (!myaudiosource.isPlaying && hitpoints >= 0)
662
+
663
+ {
664
+
665
+ int n = Random.Range(1,hurtsounds.Length);
666
+
667
+ myaudiosource.clip = hurtsounds[n];
668
+
669
+ myaudiosource.pitch = 0.9f + 0.1f *Random.value;
670
+
671
+ myaudiosource.Play();
672
+
673
+ hurtsounds[n] = hurtsounds[0];
674
+
675
+ hurtsounds[0] = myaudiosource.clip;
676
+
677
+ }
678
+
679
+ //damaged = true;
680
+
681
+ //myAudioSource.PlayOneShot(hurtsound);
682
+
683
+ hitpoints = hitpoints - damage;
684
+
685
+ }
686
+
687
+
688
+
689
+ IEnumerator equip()
690
+
691
+ {
692
+
693
+      (省略)
694
+
695
+ }
696
+
697
+ IEnumerator raycastattack()
698
+
699
+ {
700
+
701
+ yield return new WaitForSeconds(rayfiredelay);
702
+
703
+ Vector3 fwrd = mycamera.transform.forward;
704
+
705
+
706
+
707
+ Vector3 camUp = mycamera.transform.up;
708
+
709
+ Vector3 camRight = mycamera.transform.right;
710
+
711
+
712
+
713
+ Vector3 wantedvector = fwrd;
714
+
715
+ wantedvector += Random.Range( -.1f, .1f ) * camUp + Random.Range( -.1f, .1f ) * camRight;
716
+
717
+
718
+
719
+ Ray ray = new Ray (transform.position + Vector3.up,wantedvector);
720
+
721
+ RaycastHit hit = new RaycastHit();
722
+
723
+
724
+
725
+ if (Physics.Raycast(ray,out hit, 3f,mask))
726
+
727
+ {
728
+
729
+
730
+
731
+ if(hit.rigidbody) hit.rigidbody.AddForceAtPosition (force * fwrd , hit.point);
732
+
733
+ hit.transform.SendMessageUpwards ("Damage",damage, SendMessageOptions.DontRequireReceiver);
734
+
735
+ GameObject decal;
736
+
737
+
738
+
739
+ decal = Instantiate(impactprefab, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)) as GameObject ;
740
+
741
+ decal.transform.localRotation = decal.transform.localRotation * Quaternion.Euler(0f,Random.Range(-90f,90f),0f);
742
+
743
+ }
744
+
745
+
746
+
747
+
748
+
749
+ }
750
+
751
+ IEnumerator holster()
752
+
753
+ {
754
+
755
+ yield return new WaitForSeconds(.3f);
756
+
757
+ shield.parent = chestposshield;
758
+
759
+ shield.position = chestposshield.position;
760
+
761
+ shield.rotation = chestposshield.rotation;
762
+
763
+
764
+
765
+ int n = Random.Range(1,switchsounds.Length);
766
+
767
+ myaudiosource.clip = switchsounds[n];
768
+
769
+ myaudiosource.pitch = 0.9f + 0.1f *Random.value;
770
+
771
+ myaudiosource.Play();
772
+
773
+ switchsounds[n] = switchsounds[0];
774
+
775
+ switchsounds[0] = myaudiosource.clip;
776
+
777
+
778
+
779
+
780
+
781
+ weapon.parent = chestposweapon;
782
+
783
+ weapon.position = chestposweapon.position;
784
+
785
+ weapon.rotation = chestposweapon.rotation;
786
+
787
+
788
+
789
+
790
+
791
+ }
792
+
793
+
794
+
795
+ }
796
+
797
+ ```

4

樽アセットスクリプト

2018/04/29 18:23

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -53,3 +53,99 @@
53
53
 
54
54
 
55
55
  ![イメージ説明](3da95404018f7262b304005fb9467b09.jpeg)
56
+
57
+
58
+
59
+ ### 追記③
60
+
61
+
62
+
63
+ 樽アセットのスクリプト。
64
+
65
+ ```C#
66
+
67
+ using UnityEngine;
68
+
69
+ using System.Collections;
70
+
71
+
72
+
73
+ public class crate : MonoBehaviour {
74
+
75
+ public float hitpoints = 100f;
76
+
77
+ public Transform spawnobject;
78
+
79
+ public float radius = 3.0f;
80
+
81
+ public float power = 100.0f;
82
+
83
+ // Use this for initialization
84
+
85
+ void Start () {
86
+
87
+
88
+
89
+ }
90
+
91
+
92
+
93
+ // Update is called once per frame
94
+
95
+ void Update ()
96
+
97
+ {
98
+
99
+ if (hitpoints <= 0)
100
+
101
+ {
102
+
103
+ Instantiate(spawnobject, transform.position, transform.rotation);
104
+
105
+ Vector3 explosionPos = transform.position;
106
+
107
+ Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);
108
+
109
+ foreach (Collider hit in colliders)
110
+
111
+ {
112
+
113
+ if (hit.GetComponent<Rigidbody>() != null)
114
+
115
+ {
116
+
117
+ Rigidbody rb = hit.GetComponent<Rigidbody>();
118
+
119
+ rb.AddExplosionForce(power, explosionPos, radius, 3.0f);
120
+
121
+
122
+
123
+ }
124
+
125
+ }
126
+
127
+ Destroy (gameObject);
128
+
129
+ }
130
+
131
+
132
+
133
+ }
134
+
135
+ void Damage (float damage)
136
+
137
+ {
138
+
139
+
140
+
141
+
142
+
143
+ hitpoints = hitpoints - damage;
144
+
145
+ }
146
+
147
+ }
148
+
149
+
150
+
151
+ ```

3

修正

2018/04/27 17:35

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
File without changes

2

修正

2018/04/27 14:33

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -30,7 +30,17 @@
30
30
 
31
31
 
32
32
 
33
- ### 追記
33
+ ### 追記
34
+
35
+ アセットは以下です。
36
+
37
+ 現在、ダウンロードできないかもしれません。
38
+
39
+ https://www.assetstore.unity3d.com/jp/#!/content/48768
40
+
41
+
42
+
43
+ ### 追記②
34
44
 
35
45
 
36
46
 

1

修正

2018/04/27 14:32

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -27,3 +27,19 @@
27
27
  もしくは剣だけにコライダー設置したいのですが、何か良い方法はありますか?
28
28
 
29
29
  ご教示のほどよろしくお願いします。
30
+
31
+
32
+
33
+ ### 追記
34
+
35
+
36
+
37
+ spine_2を調べたら、swordの下にswordcolliderというコライダーらしきゲームオブジェクトを見つけました。
38
+
39
+ しかし、インスペクタを表示してみると、コライダーがアタッチされていません。
40
+
41
+ もうちょっと調べてみようと思います。
42
+
43
+
44
+
45
+ ![イメージ説明](3da95404018f7262b304005fb9467b09.jpeg)