简体   繁体   中英

How can I create a 3d cube and position it in the center of another object?

Adding a cube is just for testing how to find the center the blue thing. After finding the center I will use the laser beam I have.

The other object don't have renderer so I'm trying to use the rigidbody component on him to find the center.

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;
            }
    }
}

This is a screenshot of the agent with the inspector settings. The main goal is to shoot make a laser beam coming out of the center of the agent out of the circle blue thing in the center. but the blue in the center is not a bone or a gameobject I can get it's position. So I assume the blue thing is in the center.

the problem is that the cube is never in the center but somewhere else in the world.

在此处输入图像描述

In case no one give you a better answer try this:

add an empty game object and try to position it in the center of the blue circle, then you can get it's position through script.

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 circle, and you can get it's position in relative to the sci fi drone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM