Compute Capsule from Center1 and Center 2 positions

Hi,

I need to create capsule collider objecs on script. I have radius, center 1 and center 2 positions defined. Center 1 and 2 are Vector3 positions.

Do you know somebody how can I create and transform capsule from these positions? Capsule in Unity is defined by center position, direction and height.

[11765-\capsulecollider.png|11765]

I thing that height I can compute with following code:

var c = new GameObject();
c.name = "CapsuleCollider";
var cc = c.AddComponent<CapsuleCollider>();
cc.radius = radius;
cc.height = (center1 - center2).magnitude;

But I do not know how can I compute direction and center position described here http://docs.unity3d.com/Documentation/Components/class-CapsuleCollider.html.

Thanks

The center would simply be the average of center1 and center2. The direction on the other hand can not be converted that easily. The capsulecollider’s direction property has only three possibilities. One for each of the game objects local axis. This will not match the direction given by center1 and center2 in general. I’d suggest to place the collider on a child object and set its local rotation up in order to match the direction.

Talking in code:

var parent = new GameObject();
var child = new GameObject();
child.parent = parent;
var col = child.AddComponent<CapsuleCollider>();
col.radius = radius;
col.height = (center1 - center2).magnitude;
col.center = (center1 + center2)*0.5;
col.direction = 2; //z-axis
child.localRotation = Quaternion.SetLookRotation(center2 - center1);