簡體   English   中英

Unity 3D-對象位置及其大小

[英]Unity 3D - Object position and its size

我的Unity3D項目存在以下問題。 我將嘗試在圖像上進行描述。

第一張圖片

在此處輸入圖片說明

因此,我想將對象合並為1。當對象1和2碰撞時,它們將合並(對象2成為對象1的子對象)。 在實踐中,當對象1中的螺栓(這個藍色的“東西”是螺栓)與對象2碰撞時,它們應該合並。 我想將對象2放置在對象1的頂部(說頂部,我的意思是紅線在哪里,第二張圖片中的右圖)。 因此,我決定將第二個對象的localPosition設置為與bolt的localPosition相等(螺栓也是對象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是圖片中的第二個對象
  • gameObject是螺栓(藍色物體在1個對象中)
  • 腳本附在螺栓中(圖片上為藍色)

只需使用第一個對象的位置和bounds.size:

vector3 posOfSecObject =新的vector3(0,0,0);

posOfSecObject.y + = FIRSTOBJECT.GetComponent()。Renderer.bounds.size.y;

我使用了Y軸,我不知道您需要哪一個,請嘗試;)我使用此代碼來建造一棟由地板組成的房屋

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM