Skip to main content
1 of 3
Ugo Hed
  • 281
  • 3
  • 21

Rotation Problem in Mario galaxy like controller

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, 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(objectToRotate.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 child only in Y toward my new calculated vector...

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

objectToRotate.localRotation = DirObject(objectToRotate.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); } 
Ugo Hed
  • 281
  • 3
  • 21