简体   繁体   English

如何在预制孩子上设置重心?

[英]How to set center of gravity on prefab child?

I have a 3d model prefab in unity that I'm trying to add physics to.我有一个统一的 3d model 预制件,我正在尝试添加物理。 The issue is that the prefab was imported from auto desk and when that happened the pivot/center of gravity for all the child mesh objects was not where you would necessarily expect them.问题是预制件是从自动桌面导入的,当发生这种情况时,所有子网格对象的枢轴/重心不在您所期望的位置。 I've tried attaching a rigid body to the various child objects and setting the center of mass with a script but it doesn't seem to be working as I would expect.我尝试将刚体附加到各种子对象并使用脚本设置重心,但它似乎并没有像我预期的那样工作。 My method for setting the center of mass involved creating two child game objects at either end of the highlighted object and using the midpoint formula to a 'good enough' center of mass.我设置质心的方法包括在突出显示的 object 的任一端创建两个子游戏对象,并使用中点公式确定“足够好”的质心。 Is there any way around this beyond rebuilding/re-importing the object?除了重建/重新导入 object 之外,还有什么方法可以解决这个问题?

Showing the off center pivot显示偏心 pivot

Showing the weird center of mass显示奇怪的质心

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CenterOfGravity : MonoBehaviour
{
    // Start is called before the first frame update
    public Transform[] centerPoints;
    public Rigidbody rb;
    void Start()
    {
        centerPoints = gameObject.GetComponentsInChildren<Transform>();


        Transform center1 = Array.Find(centerPoints, elem => elem.name.Equals("Center1"));
        Transform center2 = Array.Find(centerPoints, elem => elem.name.Equals("Center2"));


        Vector3 center = (center1.localPosition + center2.localPosition)/2.0f;

        rb = GetComponent<Rigidbody>();
        rb.ResetCenterOfMass();
        rb.centerOfMass = center;

    }

    // Update is called once per frame
    void Update(){}
}

Every time I have to change the center off mass of an object, I create an empty child Game Object in the place where I want to be the center of mass.每次我必须更改 object 的质心时,我都会在我想成为质心的地方创建一个空子游戏 Object。

After that, I set the center off mass of the object, to the local position of the new child object, something like that:之后,我将 object 的中心偏离质量设置为新子 object 的局部 position,如下所示:

    public GameObject childObj, myObj;

    void Start()
    {
      myObj.GetComponent<Rigidbody>().centerOfMass = childObj.transform.localPosition;
    }

Edit:编辑:

If you want to change the pivot of a GameObject, you need to do those things:如果要更改 GameObject 的 pivot,则需要执行以下操作:

1-Create an empty game object and name it as "Pivot". 1-创建一个空游戏object并将其命名为“Pivot”。

2-Place the pivot where you want the center of mass to be. 2 将 pivot 放置在您想要的质心位置。

3-Asign your initial game object that you want to change the pivot as child of your new object called "Pivot". 3-指定您的初始游戏 object,您希望将 pivot 更改为您的新 object 的子级,称为“Pivot”。

Now the pivot of the initial game object should be the empty game object called pivot.现在初始游戏object的pivot应该是名为pivot的空游戏object。

Some disclaimers first, AFAIK there's no way to do that in unity BUT.首先是一些免责声明,AFAIK 没有办法统一但是。 there is a workaround I came across when I had a similar issue, The thing is that I'm not quite sure what your work pipeline is.当我遇到类似问题时,我遇到了一种解决方法,问题是我不太确定您的工作管道是什么。 and this fix might be even worst than just modifying the meshes in auto desk and reimporting it.这个修复可能比仅仅修改自动桌面中的网格并重新导入它更糟糕。

The idea is to move your object temporarily to another game object, then modifying the broken pivot and bringing it back to the original object.想法是将您的 object 暂时移动到另一个游戏 object,然后修改损坏的 pivot 并将其恢复为原始 ZA8CFDE6331BD59EB66AC946。 The idea comes from this video .这个想法来自这个视频 It may work for you too!它也可能对你有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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