在球体之间绘制保持相同尺寸的圆柱体以产生对撞机 [英] Draw cylinders between spheres keeping the same dimension to have a collider

查看:112
本文介绍了在球体之间绘制保持相同尺寸的圆柱体以产生对撞机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点卡住了.我试图在两个球体之间绘制一个圆柱体,同时在Game View中保持其在Z轴上的大小始终相同.目的是通过关闭圆柱体的网格渲染器"(Mesh Renderer)使其不可见,从而使围绕小控件线的碰撞器成为可能.用LineRenderer替换Gizmos线也很好,但我总是需要它的大小与两侧的球体相同.

I'm a little stuck. I'm trying to draw a cylinder between two spheres while keeping its size always the same on the Z axis in Game View. The aim is to get a collider around the gizmos line by turning off the cylinder's Mesh Renderer to make it invisible. It would also be good to replace the Gizmos line with a LineRenderer but I would always need it to be the same size as the sphere on both sides.

这是我的代码:

GameObject newSphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
// currentPosPoint = mouse click
newSphere.transform.position = currentPosPoint; 
// this way all the spheres appear of the same size even if in a different position on the Z axis
newSphere.transform.localScale = Vector3.one * ((new Plane(cam.transform.forward, 
                                                           cam.transform.position).GetDistanceToPoint(
                                                           newSphere.transform.position)) / radiusPoint);
// if the sphere is not the first
if (count > 1) {
    Vector3 start = posSphere1;
    Vector3 end = posSphere2;
    GameObject newCyl = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
    // I find the central position between the two spheres
    Vector3 pos = Vector3.Lerp(start, end, 0.5f);
    newCyl.transform.position = pos;
    // I find the rotation of the cylinder
    Vector3 dirV = Vector3.Normalize(end - start);
    Vector3 cylDefaultOrientation = new Vector3(0,1,0);
    Vector3 rotAxisV = dirV + cylDefaultOrientation;
    rotAxisV = Vector3.Normalize(rotAxisV);
    newCyl.transform.rotation = new Quaternion(rotAxisV.x, rotAxisV.y, rotAxisV.z, 0);
    float dist = Vector3.Distance(end, start);
    // it is from this point that I cannot get out of it
    // I resize the cylinder like the sphere
    newCyl.transform.localScale = Vector3.one * ((new Plane(cam.transform.forward, 
                                                            cam.transform.position).GetDistanceToPoint(
                                                            newCyl.transform.position)) / radiusPoint);
    // I assign the length of the cylinder to join its two ends to the spheres
    Vector3 newScale = newCyl.transform.localScale;
    newScale.y= dist/2;
    newCyl.transform.localScale = newScale;      
}

如您所见,只有在X轴上定向圆柱体时,圆柱体才与球体相同.

As you can see the cylinders are the same size as the sphere only if they are oriented on the X axis.

您对始终获得相同尺寸的气瓶有什么建议吗? 正如我说过的,不需要使用圆柱体,目标是在Gizmos线周围放置一个对撞机,任何更改都将是很好的.

Do you have any suggestions to always get the same size of cylinders? As I said it is not needed to use cylinders, the target is to get a collider around the gizmos line, any change would be good.

推荐答案

将对象与LineRenderer一起使用,并根据起点和终点位置设置起点/终点宽度.

Use an object with a LineRenderer, and set the start/endwidth based on the start and end positions.

GameObject go = new GameObject("LineRenderer Object")
LineRenderer lr = go.AddComponent<LineRenderer>() as LineRenderer;

Vector3 start = posSphere1;
Vector3 end = posSphere2;

Plane camPlane = new Plane(cam.transform.forward, cam.transform.position)

float startWidth = camPlane.GetDistanceToPoint(start)) / radiusPoint);
float endWidth = camPlane.GetDistanceToPoint(end)) / radiusPoint);

// Make sure line renderer width curve is linear
lr.widthCurve = AnimationCurve.Linear(0f, startWidth , 1f, endWidth);

确保将GameObject放置在数组中的某个位置,以便可以Destroy放置.

Make sure you put the GameObject somewhere like in an array so you can Destroy it.

这篇关于在球体之间绘制保持相同尺寸的圆柱体以产生对撞机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆