简体   繁体   中英

When rotating an object in Unity, the script doesn't rotate the object on the Z-axis, only the X and Y?

I have an rectangular cube that needs to be placed between two other objects, and it properly rotates so that each end faces the two objects. Since this happens on a spherical world, the issue lies when I try to then align it to be properly facing "up". I didn't realize this before, but this code (which works on the other objects as they only need to orient once) only uses the X and Y axes. I can manually orient them properly by modifying the Z-axis in the Editor, so I know it's possible, but since they must be created procedurally that obviously isn't feasible. Changing from transform.up to down or right changes absolutely nothing.

In the following code, parent is a one of the objects it has to orient towards (which it does fine), and target is the center of the sphere.

In theory, the alignment to the two side objects should only need to use the X and Y axes, and the alignment to the center of the sphere then only needs to use the Z axis as represented in the final line of code, yet in practice this doesn't work as both alignments both want to use only the X and Y axes and ignore the Z axis.

        Vector3 parentDirection = parent.transform.position - transform.position;
    Vector3 targetDirection = target.position - transform.position;

    Vector3 DirectionToParent = Vector3.RotateTowards(transform.forward, parentDirection, 100f, 0.0f);
    Vector3 CenterDiretion = Vector3.RotateTowards(transform.forward, targetDirection, 100f, 0.0f);

    Quaternion ParentRotation = Quaternion.LookRotation(DirectionToParent );
    Quaternion CenterRotation = Quaternion.LookRotation(CenterDiretion );

    transform.rotation = Quaternion.Euler(DirectionToParent.eulerAngles.x, DirectionToParent.eulerAngles.y, newRotation.eulerAngles.z);

The code shown in the question doesn't work because it doesn't take into account the direction to/away the center of the planet when calculating transform.rotation .

Instead, I would recommend just using Quaternion.LookRotation to assign the rotation that would produce the forward you have with parentDirection, and the up closest to the up direction of the planet.

Vector3 upDirection = transform.position - target.position;
transform.rotation = Quaternion.LookRotation(parentDirection, upDirection);

And if you need to change the rotation based on some max velocity, just assign the result of Quaternion.LookRotation to a variable and Quaternion.RotateTowards from transform.rotation and assign the result to transform.rotation as necessary:

private float turnVelocity = 100f;

// ...

Vector3 upDirection = transform.position - target.position;

Quaternion goalRot = Quaternion.LookRotation(parentDirection, upDirection);
transform.rotation = Quaternion.RotateTowards(transform.rotation, goalRot, 
            turnVelocity * Time.deltaTime);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM