Unity3Dプロジェクトに次の問題があります。私はイメージでそれを説明しようとします。Unity 3D - オブジェクトの位置とサイズ
だから私は、彼らが(オブジェクト2は、対象物1の子になった)マージ、1物体1と2衝突にオブジェクトにマージしたいです。実際には、オブジェクト1のボルト(この青い「何か」がボルトです)がオブジェクト2と衝突してからマージする必要がありました。そして、オブジェクト1をオブジェクト2の上に配置したいとします(上部は赤い線がどこにあるのかを示し、右のイメージは2番目の画像です)。だから私は、2番目のオブジェクトのlocalPositionをboltのlocalPosition(ボルトはオブジェクト1の子も同じ)に設定することにしました。しかし、それは間違っていた(2番目の画像、左側)。だから私は、彼の軸の1つに第2のオブジェクトの高さの半分を加えなければならないという考えを得る。しかしそれでも完璧ではありません。また、私はそれをどの軸に追加すべきかわかりません。そして、この半分の高さを追加するときは、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));
}
}
私は、オブジェクトが異なるサイズを持つことができることを追加する必要がありますので、私はちょうど数を追加することはできません、それは柔軟でなければなりません。私はどんな助けにも非常に感謝しています。
EDIT: この私の全体のコード:
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は、ゲームオブジェクトは、スクリプトをボルト(画像上の青いもの)
で構成される家を構築するために、このコードを使用し、だから私は缶)誰が 'childObject'を知っていますか、誰がmainObjectで、どのgameObjectにこのスクリプトが添付されていますか? –
私は自分の投稿をフルコードで編集しました – Celdur