-1

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.

Platform rotation on the X is 90

The capsule rotation on the X is also 90 :

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 :

Rotation

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.

Daniel Lip
  • 1,747
  • 3
  • 34
  • 75

1 Answers1

0

You can avoid this distortion by keeping the scale - especially any non-uniform scale - at the leaf nodes of your transformation hierarchy.

So your hierachy could look like this:

  • Platform Root: local rotation = (0, 0, 0) / local scale = (1, 1, 1)
    • Cube Child: local rotation = (0, 0, 0) / local scale = (4.3, 1, 4.3)
    • Capsule Child: local rotation = (0, 0, 0) / local scale = (1, 1, 1)

Because the root has only uniform scale, rotating it and objects inside it won't cause an distortion.

DMGregory
  • 134,153
  • 22
  • 242
  • 357