This question already has an answer here:

i'm doing a Mario galaxy like CCC. And I have a serious rotation problem, may be Gimble lock, or maybe not.

So first, I have a parent object ("PLAYER") who can rotate toward a planet: https://youtu.be/j3VkemYQTa8

With this 3 line:

Vector3 dirOrientation = GetNormal() //here the normal collision
Quaternion targetRotation = Quaternion.FromToRotation(PLAYER.transform.up, dirOrientation) * PLAYER.transform.rotation;
PLAYER.transform.rotation = Quaternion.RotateTowards(PLAYER.transform.rotation, targetRotation, speedRotate);

Ok, next, I want to rotate the child object ("CHILDPLAYER"), who contain the render, according to the input of the player See in editor: https://youtu.be/4Sb8t82OT-E

So here... I managed to get the direction of the input, according to the camera. See in blue, the directionnal line: the Vector3 point to the good direction according to the player and the camera, yes ! https://youtu.be/4TvPsXp-W9U

Vector2 dirInput = playerInput.GetDirInput();//input player with the stick, X,Y
//here REFOBJECT is the camera (who dictate the "forward" of the player
Vector3 relativeDirection = REFOBJECT.right * dirInput.x + REFOBJECT .forward * dirInput.y;
Debug.DrawRay(CHILDPLAYER.position, relativeDirection, Color.cyan, 0.4f);

But now... As you see even if I have the good direction (cf blue line in the vidéo). I can't manage to rotate the child gameObject accordingly... I have to localy rotate smoothly the CHILDPLAYER only in Y toward my new calculated vector...

Here the code I use to rotate the CHILDPLAYER, with my relativeDirection Vector3:

CHILDPLAYER.localRotation = DirObject(CHILDPLAYER.localRotation, relativeDirection, turnRate);

public Quaternion DirObject(Quaternion rotation, Vector3 dir, float turnRate)
        float heading = Mathf.Atan2(-dir.x * turnRate * Time.deltaTime, dir.z * turnRate * Time.deltaTime);

        Quaternion _targetRotation = Quaternion.identity;

        float x = 0;
        float y = heading * -1 * Mathf.Rad2Deg;
        float z = 0;

        _targetRotation = Quaternion.Euler(x, y, z);
        rotation = Quaternion.RotateTowards(rotation, _targetRotation, turnRate * Time.deltaTime);
        return (rotation);

See the final result: https://youtu.be/lSQQFDDuj7U Here, I'm going ALWAYS DOWN with the stick... and even if the direction calculated in relativeDirection is good, the child player rotate itself... ...

enter image description here


marked as duplicate by Tyyppi_77, Community Jan 23 at 14:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    \$\begingroup\$ "I have to localy rotate smoothly the CHILDPLAYER only in Y toward my new calculated vector..." This sounds a lot like your previous question "Smooth local rotation around one axis, oriented toward a target" - does an answer to that question solve this one? Or do you have a remaining problem to sort out even with a solution to the first question in-hand? \$\endgroup\$ – DMGregory Jan 23 at 2:00

Thanks yo @DMGregory, here is the answers: Unity smooth local rotation around one Axis, oriented toward a target

Sorry for the duplicate, I thought this was 2 separate problem. It was not.


Not the answer you're looking for? Browse other questions tagged or ask your own question.