-1
\$\begingroup\$

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.

\$\endgroup\$
6
  • \$\begingroup\$ I recommend not using non-uniform scale on a parent object, for reasons I explain in more detail here \$\endgroup\$
    – DMGregory
    Commented May 21, 2021 at 3:18
  • \$\begingroup\$ @DMGregory Then there is no solution to my problem? or I didn't understand it. \$\endgroup\$
    – Daniel Lip
    Commented May 21, 2021 at 6:30
  • \$\begingroup\$ 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? \$\endgroup\$
    – Ermiq
    Commented May 21, 2021 at 9:32
  • \$\begingroup\$ 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. \$\endgroup\$
    – Ermiq
    Commented May 21, 2021 at 9:45
  • \$\begingroup\$ 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. 😉 \$\endgroup\$
    – DMGregory
    Commented May 21, 2021 at 10:56

1 Answer 1

0
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .