Unity3D プロジェクトに次の問題があります。画像で説明しようと思います。
オブジェクト 1 と 2 が衝突すると、それらはマージされます (オブジェクト 2 はオブジェクト 1 の子になります)。実際には、オブジェクト 1 のボルト (この青い「何か」はボルト) がオブジェクト 2 と衝突すると、それらはマージされます。そして、オブジェクト 2 をオブジェクト 1 の上に配置したいと思います (一番上とは、赤い線がどこにあるか、2 番目の画像の右の画像を意味します)。そこで、2 番目のオブジェクトの localPosition をボルトの localPosition と等しくなるように設定することにしました (ボルトもオブジェクト 1 の子です)。しかし、それは間違っていました (2 番目の画像、左側)。そのため、2 番目のオブジェクトの高さの半分をその軸の 1 つに追加する必要があると考えました。しかし、まだ完璧ではありません。また、どの軸に追加すればよいかわかりません。この半分の高さを追加するときは、localPosition または通常の位置を使用する必要がありますか?
私のコード:
void OnTriggerEnter(Collider c) {
if(transform.IsChildOf(mainObject.transform)) {
childObject.transform.SetParent (mainObject.transform);
childObject.transform.localPosition = boltObject.transform.localPosition;
childObject.transform.position = new Vector3 (childObject.transform.position.x, childObject.transform.position.y, (float)(childObject.transform.position.z + childObject.GetComponent<Renderer>().bounds.size.z));
}
}
オブジェクトのサイズが異なる可能性があることを追加する必要があるため、数値を追加するだけではなく、柔軟でなければなりません。どんな助けにもとても感謝します。
編集:これは私のコード全体です:
using UnityEngine;
using System.Collections;
public class MergeWithAnother : MonoBehaviour {
public GameObject mainObject;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider c) {
if(transform.IsChildOf(mainObject.transform)) {
if (c.gameObject.name == "holeForBolt" && c.gameObject.transform.parent.gameObject != mainObject) {
Destroy (c.gameObject.transform.parent.gameObject.GetComponent("MouseDrag"));
Destroy (c.gameObject.transform.parent.gameObject.GetComponent("RotateObject"));
c.gameObject.transform.parent.gameObject.transform.SetParent (mainObject.transform);
c.gameObject.transform.parent.gameObject.transform.localPosition = gameObject.transform.localPosition;
c.gameObject.transform.parent.gameObject.transform.position = new Vector3 (c.gameObject.transform.parent.gameObject.transform.position.x, c.gameObject.transform.parent.gameObject.transform.position.y, (float)(c.gameObject.transform.parent.gameObject.transform.position.z + c.gameObject.transform.parent.gameObject.GetComponent<Renderer>().bounds.size.z));
c.gameObject.transform.parent.gameObject.transform.localRotation = gameObject.transform.localRotation;
c.gameObject.transform.parent.gameObject.transform.localRotation = new Quaternion (360 + c.gameObject.transform.parent.gameObject.transform.localRotation.x, c.gameObject.transform.parent.gameObject.transform.localRotation.y, c.gameObject.transform.parent.gameObject.transform.localRotation.z, c.gameObject.transform.parent.gameObject.transform.localRotation.w);
Destroy (c.gameObject.transform.parent.gameObject.GetComponent<CapsuleCollider>());
Destroy (gameObject);
Destroy (c.gameObject);
CapsuleCollider cc = mainObject.GetComponent<CapsuleCollider>();
cc.height *= 2;
cc.center = new Vector3(0, 0, 1);
}
}
} }
私はそれが何を意味するかを説明します:
- MainObject は、図の最初のオブジェクトです。
- c.gameObject.transform.parent.gameObject は画像の 2 番目のオブジェクトです
- gameObject はボルト (1 つのオブジェクトに青い何か)
- 台本はボルトで付いています(写真の青いもの)