Нарисуйте цилиндры между сферами, сохраняя одинаковый размер, чтобы иметь коллайдер - PullRequest
0 голосов
/ 17 мая 2019

Я немного застрял. Я пытаюсь нарисовать цилиндр между двумя сферами, сохраняя его размер всегда одинаковым на оси Z в Game View. Цель состоит в том, чтобы обвести коллайдер вокруг линии гизмо, отключив Mesh Renderer, чтобы сделать его невидимым. Также было бы хорошо заменить линию Gizmos на LineRenderer, но мне всегда нужно, чтобы она была того же размера, что и сфера с обеих сторон.

enter image description here

Это мой код:

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.

Есть ли у вас какие-либо предложения, чтобы всегда получать одинаковые размеры цилиндров? Как я уже сказал, использовать цилиндры не нужно, цель состоит в том, чтобы обвести коллайдер вокруг линии гизмо, любое изменение было бы хорошо.

1 Ответ

1 голос
/ 17 мая 2019

Используйте объект с LineRenderer и установите начальную / конечную ширину на основе начальной и конечной позиций.

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 это.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...