[英]How can I create a 3d cube and position it in the center of another object?
添加一個立方體只是為了測試如何找到藍色物體的中心。 找到中心后,我將使用我擁有的激光束。
另一個 object 沒有渲染器,所以我試圖使用他身上的剛體組件來找到中心。
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.AI;
public class CenterTest : MonoBehaviour
{
public NavMeshAgent agent;
private bool createCube = false;
void Start()
{
}
void Update()
{
if (createCube == false)
{
var center = agent.GetComponent<Rigidbody>().centerOfMass;
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.localPosition = new Vector3(0, 0, 0);//center;
cube.transform.parent = agent.transform;
createCube = true;
}
}
}
這是帶有檢查器設置的代理的屏幕截圖。 主要目標是從中心的藍色圓圈中射出從代理中心射出的激光束。 但中間的藍色不是骨頭或游戲對象,我可以得到它是 position。 所以我假設藍色的東西在中心。
問題是立方體永遠不在中心,而是在世界的其他地方。
如果沒有人給你更好的答案,試試這個:
添加一個空游戲 object 並在藍色圓圈的中心嘗試 position ,然后通過腳本可以得到它的 position。
Another method (if you familiar with 3d software) is to open the sci fi drone model in a 3d software and set the position to (0,0,0), then switch to vertices mode and select the vertex in the middle of the blue圈子,你可以得到它相對於科幻無人機的 position。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.