微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当原点不在网格上时,如何将网格/游戏对象放置在相机的 FoV 中间?

如何解决当原点不在网格上时,如何将网格/游戏对象放置在相机的 FoV 中间?

我正在制作一个 Unity 游戏,我需要在相机 FoV 中心显示从服务器下载的 3D 模型,问题是某些模型在网格上没有枢轴,所以我的代码在这种情况下不能很好地工作:

    Vector3 positionInFront =  CameraCache.Main.transform.position + (CameraCache.Main.transform.forward * 1); 
    transform.position = positionInFront ;   

知道如何在不使用父级/更改层次结构的情况下解决此问题吗? 这解决了我的问题,但是当模型在 polycount 方面很大时,应用程序很难在层次结构上执行更改:

    GameObject aux = new GameObject();
    aux.transform.position = targetCenter;
    transform.SetParent(aux.transform);
    aux.transform.position = positionInFront;
    transform.transform.SetParent(null);
    GameObject.Destroy(aux);

解决方法

您可以使用几何图形的中心,例如Renderer.bounds.center 以便计算移动对象所需的增量向量,使中心最终位于目标位置,例如

// Model pivot in worldspace
var position = yourModel.position;
// Center if geometry in worldspace
var center = yourModel.GetComponent<Renderer>().bounds.center;
// vector from center to pivot
var centerToPositionDelta = position - center;
// Your target position in worldspace
var positionInFront =  CameraCache.Main.transform.position + CameraCache.Main.transform.forward * 1; 
// By adding the shifting vector center -> position
// you move the object just so the center ends up in the targetPosition
transform.position = positionInFront  + centerToPositionDelta;   

在智能手机上打字,但我希望思路清晰

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。