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... ...