# How can I spin a child capsule of a cube parent?

I have a parent cube and to make it like a surface I had to change the rotation of the cube on the X to 90. and as a child, I added a capsule, and to make the capsule-like standing I had to change the capsule X also to 90. The result is :

The reason I put the capsule child of the cube is that I want the capsule to move with the cube.

The capsule rotation on the X is also 90 :

Then in a script at the top, I added speed and index variables for the rotation :

private int index = 0;
private int rotationIndex = 0;


In the Update :

RotateTo();


And the method : curvedLinePoints is a List

private void RotateTo()
{
var distance = Vector3.Distance(capsule.position, curvedLinePoints[rotationIndex].transform.position);
if(distance < 0.1f)
{
rotationIndex++;
}

// Determine which direction to rotate towards
Vector3 targetDirection = curvedLinePoints[rotationIndex].transform.position -capsule.position;

// The step size is equal to speed times frame time.
float singleStep = rotationSpeed * Time.deltaTime;

// Rotate the forward vector towards the target direction by one step
Vector3 newDirection = Vector3.RotateTowards(capsule.forward, targetDirection, singleStep, 0.0f);

// Draw a ray pointing at our target in
Debug.DrawRay(capsule.position, newDirection, Color.red);

// Calculate a rotation a step closer to the target and applies rotation to this object
capsule.rotation = Quaternion.LookRotation(newDirection);
}


The problem is that because the capsule is a child when I rotate the capsule it looks like the capsule changes its shape and not spinning around itself. If I remove ut the capsule from the prefab and will rotate it on its own it will spin fine but when it's a child of the cube the result is :

I tried before running the game to play with the capsule rotation in the editor in the inspector changing the Y and Z values and the result is like in this screenshot. but I want to spin it around itself but because it's a child of the cube it's not spinning but looks like it's changing its shape.

The workaround I found is to create an empty GameObject at 0,0,0 and put both Platform and Capsule as a child of it.

Then I added this part to the script above :

private void LateUpdate()
{
capsule.localPosition = transform.localPosition + new Vector3(0, 1.4f, 0);
}


This way the Capsule is following the Platform. The empty GameObject is just to put them both together.

and for testing I just did in the Update :

capsule.Rotate(0, 10, 0);


and it's working fine. Now I have to figure out how to make this rotation with the code in the RotateTo method.

• – DMGregory May 21 at 3:18
• @DMGregory Then there is no solution to my problem? or I didn't understand it. – Shitrit Lehava May 21 at 6:30
• Why didn't you just set the cube's scale to 43, 1, 43 to get the same platform-like shape without the additional rotation around X axis? – Ermiq May 21 at 9:32
• To make the capsule keep standing on the platform you don't have to make it a child of the platform. Just update the capsule position relative to the platform position in Update(). E.g.: capsule.transform.position = platform.transform.position + Vector3.up * (platform.transform.localScale.y * 0.5f + capsule.transform.localScale.y * 0.5f); Or better use collider.bounds.y * 0.5f to get the real Y size of the platform/capsule. – Ermiq May 21 at 9:45
• There is a solution. Make the cube a child of the "platform" while the platform object itself is an empty game object with identity scale/rotation. I'll elaborate a bit more after the work day is done unless someone else beats me to it. 😉 – DMGregory May 21 at 10:56