質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

2769閲覧

コライダーが設定しづらい

退会済みユーザー

退会済みユーザー

総合スコア0

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

1クリップ

投稿2018/04/27 12:13

編集2018/04/29 18:23

前提・実現したいこと

図のようにシーン上に配置しているとき、キャラクターは直立していて、
後ろの剣にコライダーを設置したいのですが、設置しにくい状況です。

イメージ説明

ゲームを再生すると、キーを押すことでモデルのポーズが変更できるのですが、
ゲーム再生中なので、この時にコライダーを設置しても意味ありません。

イメージ説明

このポーズをしている状態で、コライダーを設置する方法はありますか?
もしくは何か良い方法はありますか?
ご教示のほどよろしくお願いします。

追記①

アセットは以下です。
現在ダウンロードできないかもしれません。
https://www.assetstore.unity3d.com/jp/#!/content/48768

追記②

spine_2を調べたら、swordの下にswordcolliderというコライダーらしきゲームオブジェクトを見つけました。
しかし、インスペクタを表示してみると、コライダーがアタッチされていません。

イメージ説明

追記③

樽アセットのスクリプト。

C#

1using UnityEngine; 2using System.Collections; 3 4public class crate : MonoBehaviour { 5 public float hitpoints = 100f; 6 public Transform spawnobject; 7 public float radius = 3.0f; 8 public float power = 100.0f; 9 void Start () { 10 11 } 12 13 void Update () 14 { 15 if (hitpoints <= 0) 16 { 17 Instantiate(spawnobject, transform.position, transform.rotation); 18 Vector3 explosionPos = transform.position; 19 Collider[] colliders = Physics.OverlapSphere(explosionPos, radius); 20 foreach (Collider hit in colliders) 21 { 22 if (hit.GetComponent<Rigidbody>() != null) 23 { 24 Rigidbody rb = hit.GetComponent<Rigidbody>(); 25 rb.AddExplosionForce(power, explosionPos, radius, 3.0f); 26 27 } 28 } 29 Destroy (gameObject); 30 } 31 32 } 33 void Damage (float damage) 34 { 35 36 37 hitpoints = hitpoints - damage; 38 } 39} 40

追記④

キャラクターにアタッチされているスクリプト。

using UnityEngine; using System.Collections; using UnityEngine.UI; public class simplecontroller : MonoBehaviour { public Transform mycamera; private Transform reference; public float jumpHeight = 2.0f; public float jumpinterval = 1.5f; private float nextjump = 1.2f; private float maxhitpoints = 1000f; private float hitpoints = 1000f; public float regen = 100f; public AudioClip[] hurtsounds; public AudioClip[] jumpsounds; public AudioClip[] attacksounds; public float gravity = 20.0f; public float rotatespeed = 4.0f; private float wantedrotatespeed; public float normalspeed = 4.0f; public float runspeed = 8.0f; public float dampTime = 2.0f; private bool isattack; public Transform target; private float moveAmount; public float smoothSpeed = 2.0f; private Vector3 forward = Vector3.forward; private Vector3 moveDirection = Vector3.zero; private Vector3 right; private float movespeed; public Vector3 localvelocity; public bool running = false; public bool canrun = true; AudioSource myaudiosource; Vector3 targetDirection = Vector3.zero; Vector3 targetVelocity; public GameObject impactprefab; public LayerMask mask; public float rayfiredelay = 0.35f; public float force = 1000f; public float damage = 50f; public Transform Deadprefab; Animator animator; CharacterController controller; Quaternion oldrotation; Quaternion currentrotation; public int attackanimcount; private int prevrandom = 0; private float randomattackfloat; public Transform shield; public Transform weapon; public Transform lefthandpos; public Transform righthandpos; public Transform chestposshield; public Transform chestposweapon; public AudioClip[] switchsounds; public AudioClip[] wooshsounds; private float addfloat = 0f; private bool fightmodus = false; private bool didselect; private bool canattack= false; void Awake () { myaudiosource = GetComponent<AudioSource>(); myaudiosource.loop = false; reference = new GameObject().transform; animator = GetComponent<Animator>(); controller = GetComponent<CharacterController>(); } void Start () { Cursor.lockState = CursorLockMode.Locked; } void Update () { reference.eulerAngles = new Vector3(0, mycamera.eulerAngles.y, 0); forward = reference.forward; right = new Vector3(forward.z, 0, -forward.x); float hor = Input.GetAxis("Horizontal") * 5f * Time.deltaTime; float ver = Input.GetAxis("Vertical") * 5f * Time.deltaTime; Vector3 velocity = controller.velocity; localvelocity = transform.InverseTransformDirection(velocity); bool ismovingforward =localvelocity.z > .5f; targetDirection = (hor * right) + (ver * forward); targetDirection = targetDirection.normalized; targetVelocity = targetDirection; if (fightmodus) { if (targetDirection == Vector3.zero) { Vector3 localTarget = transform.InverseTransformPoint (target.position); addfloat = (Mathf.Atan2 (localTarget.x, localTarget.z)); } else { addfloat = 0f; } if (running) { if (targetDirection != Vector3.zero) {(省略) } } else {(省略) } } else { addfloat = 0f; lookweight = 0.1f; if (targetDirection != Vector3.zero) { var lookrotation2 = Quaternion.LookRotation(targetDirection,Vector3.up); lookrotation2.x = 0; lookrotation2.z = 0; transform.rotation = Quaternion.Lerp(transform.rotation,lookrotation2,Time.deltaTime * wantedrotatespeed); } } if (Input.GetButtonDown("Fire3") && Time.time > nextselect && canequip) { StartCoroutine (weaponselect ()); nextselect = Time.time + 2f; } if (canattack && controller.isGrounded) { bool attackState = animator.GetCurrentAnimatorStateInfo(1).IsName("attacks"); if (attackState) { canjump = false; //animator.applyRootMotion = true; } else { canjump = true; //animator.applyRootMotion = false; } if (Input.GetButtonDown("Fire1")) { if (!attackState) { StartCoroutine(raycastattack()); int randomattack = randomIntExcept (0, attackanimcount, prevrandom); randomattackfloat = randomattack * 1.0f; animator.SetBool ("attack", true); animator.SetFloat ("random", randomattackfloat); int n = Random.Range (1, attacksounds.Length); myaudiosource.clip = attacksounds [n]; myaudiosource.pitch = 0.9f + 0.1f * Random.value; myaudiosource.Play (); attacksounds [n] = attacksounds [0]; attacksounds [0] = myaudiosource.clip; prevrandom = randomattack; } } else { animator.SetBool("attack",false); } } if (controller.isGrounded) { canequip = true; lastspeed = Mathf.Lerp(lastspeed,speed,Time.deltaTime * 3f); if (Input.GetButton ("Jump") && canjump && Time.time > nextjump) { (省略) } } else { canequip = false; moveDirection.y -= (gravity) * Time.deltaTime; nextjump = Time.time + jumpinterval; animator.SetBool ("jump",false); } if (Input.GetButton ("Fire2") && canrun && ismovingforward) { speed = runspeed; running = true; wantedrotatespeed = rotatespeed; } else { speed = normalspeed; running = false; wantedrotatespeed = rotatespeed; } targetVelocity *= lastspeed; moveDirection.z = targetVelocity.z; moveDirection.x = targetVelocity.x; animator.SetFloat("hor",((localvelocity.x/normalspeed) + addfloat), dampTime , 0.8f); animator.SetFloat("ver",((localvelocity.z/normalspeed)), dampTime , 0.8f); if (hitpoints <= 0) { //die Instantiate(Deadprefab, transform.position, transform.rotation); Destroy(gameObject); } animator.SetBool("grounded",controller.isGrounded); controller.Move (moveDirection * Time.deltaTime); if (hitpoints < maxhitpoints) hitpoints += regen * Time.deltaTime; } void Damage (float damage) { if (!myaudiosource.isPlaying && hitpoints >= 0) { int n = Random.Range(1,hurtsounds.Length); myaudiosource.clip = hurtsounds[n]; myaudiosource.pitch = 0.9f + 0.1f *Random.value; myaudiosource.Play(); hurtsounds[n] = hurtsounds[0]; hurtsounds[0] = myaudiosource.clip; } //damaged = true; //myAudioSource.PlayOneShot(hurtsound); hitpoints = hitpoints - damage; } IEnumerator equip() {      (省略) } IEnumerator raycastattack() { yield return new WaitForSeconds(rayfiredelay); Vector3 fwrd = mycamera.transform.forward; Vector3 camUp = mycamera.transform.up; Vector3 camRight = mycamera.transform.right; Vector3 wantedvector = fwrd; wantedvector += Random.Range( -.1f, .1f ) * camUp + Random.Range( -.1f, .1f ) * camRight; Ray ray = new Ray (transform.position + Vector3.up,wantedvector); RaycastHit hit = new RaycastHit(); if (Physics.Raycast(ray,out hit, 3f,mask)) { if(hit.rigidbody) hit.rigidbody.AddForceAtPosition (force * fwrd , hit.point); hit.transform.SendMessageUpwards ("Damage",damage, SendMessageOptions.DontRequireReceiver); GameObject decal; decal = Instantiate(impactprefab, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)) as GameObject ; decal.transform.localRotation = decal.transform.localRotation * Quaternion.Euler(0f,Random.Range(-90f,90f),0f); } } IEnumerator holster() { yield return new WaitForSeconds(.3f); shield.parent = chestposshield; shield.position = chestposshield.position; shield.rotation = chestposshield.rotation; int n = Random.Range(1,switchsounds.Length); myaudiosource.clip = switchsounds[n]; myaudiosource.pitch = 0.9f + 0.1f *Random.value; myaudiosource.Play(); switchsounds[n] = switchsounds[0]; switchsounds[0] = myaudiosource.clip; weapon.parent = chestposweapon; weapon.position = chestposweapon.position; weapon.rotation = chestposweapon.rotation; } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

negitama

2018/04/27 13:23

このモデルって無料アセットですよね。もしそうであれば、参考情報としてアセットの情報を追記したほうが回答者とコミュニケーションしやすいかもしれませんね。商品の宣伝になってはまずいですが。
negitama

2018/04/27 13:42

情報ありがとうございます。もとの質問内容のほうを更新して、追記したおいたほうがいいかも。現在ダウンロードできないという情報も含めて。
guest

回答1

0

ベストアンサー

質問者さんの挙げているアセットが現在入手できないようなので、取り急ぎ。

画像の2枚目のツリーのなかに、武器に関するボーンはありませんか?(Weapon、Sword、WeaponHolderなど)
もしあれば、そこにコライダーを追加して位置調整すればOKだと思います。

なければ、spine を展開すると右手のボーンが出てくると思うので、そこに空のゲームオブジェクトをぶら下げて(Weaponなどと名前を付ける)、それにコライダーをアタッチして試してみてください。

参考:
[Physics] 敵の武器に当たり判定を付ける [Unity] : ねぎたまらぼ(Unity, C#, VR)

投稿2018/04/27 13:51

negitama

総合スコア943

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2018/04/27 14:35

ご回答ありがとうございます。 空のゲームオブジェクトをぶら下げて、それにコライダーをアタッチする方法もあるのですね。 調べてみたら、コライダーに関係しそうなオブジェクトを見つけました。 追記しました。
negitama

2018/04/27 14:38

SwordCollider という空のゲームオブジェクトがあるのなら、それにカプセルコライダーでもアタッチしてみて、動作確認してみてください。 アニメーションさせてみて、コライダーが武器のモデルに追従しているようなら、コライダーの位置を微調整すればOKだと思います。
退会済みユーザー

退会済みユーザー

2018/04/27 17:34

ご回答ありがとうございます。 カプセルコライダーをアタッチして追従するようになりました。 また、今頃気づいて申し訳ないのですが、このアセットで用意されている樽みたいなアセットに、剣の方はコライダー無しで、その剣を振り回して当ててみたら、樽が破壊されるアクションが実装されていました。 剣の方にはコライダーがないはずなのに、樽にヒットして(動きとしてはすり抜けている)、剣が当たっていることを検知しているのはどうゆうカラクリなのかよくわかりませんが、おそらくスクリプトで衝突も検知できるのかもしれないと思いました。 ご参考になるかはわかりませんが、樽のアセットにアタッチされていたスクリプトも貼っておきます。もしも、このスクリプトが衝突の検知に関係していたら、ご教示いただきたいです。
negitama

2018/04/27 17:58

追記③の crate クラスには、「(樽が)ダメージを受ける」「樽のヒットポイントが0以下になったら爆発する」というような感じの処理しか書かれていないようです。 ここからは推測ですが、樽ではなくて、このキャラクター側にアタッチされているスクリプト側で、swordcollider の位置にコライダー(を含むプレハブ)を生成する処理が書かれているのでしょうね。剣をしっかり振っているモーション中のある区間だけ、コライダーを有効にする、などの処理を行っているのかもしれません。
退会済みユーザー

退会済みユーザー

2018/04/29 18:28

遅くなって申し訳ありません。 どうしても気になってコライダー生成のスクリプトを探しているのですが、見つかりませんでした。 キャラクターにアタッチしていたsimplecontrollerを追記しましたが(teratailの文字数制限上、そのクラスのコードを全て掲載できなかった為、攻撃とは関係なさそうなコードは省きました。)、 このsimplecontrollerのraycastattack()というメソッドが、コライダーを生成せずに攻撃を実装しているコードなのかもしれないと思ったのですが、いかがでしょうか?
negitama

2018/04/30 02:47

その箇所ですね。simplecontrollerのraycastattack()のなかで、樽にダメージを与える処理が行われています。ただしコライダーは生成していないようです(少なくとも追記4のコード内では)。代わりに、キャラクターの位置から指定の向きに最大長3のRay(光線)を飛ばして、そのRayが樽に当たったら、樽のスクリプト側のDamageメソッドを呼び出す、という処理になっているようですね。 (掲題の質問から離れてきているので、この回答か次あたりで切り上げさせてもらいますね)
退会済みユーザー

退会済みユーザー

2018/04/30 02:55

ご回答ありがとうございます。 コライダーをアタッチしなくても、攻撃が実装できることがわかり勉強になりました。 ご丁寧なご解説ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問