- Home /
Align move direction to a child object's facing direction
Hi, I'm currently working on using thumbsticks to move the character controller, but met some problems.
I'm having a Vector2 from the thumbstick input to move the character controller.
However, it's a VR project. I hope to use the centereye's facing direction as the forward direction.
The "Character" is the parent object with the movement scripts and character controller on it (will not rotate). The centereye camera is the child object (will rotate).
So when I am moving, because the Character doesn't rotate, the movement is always in a fixed coordinate system. I don't want the Character to rotate or it will influence the center eye camera, but I need it to move based on the centereye's facing direction as transform.forward.
So I was wondering how to modify my code so that the character object can move in the direction align with the centereye but does not rotate itself.
(I've tried transform.InverseTransformDirection and transform.TransformDirection but saw no difference)
Thankyou.
void Update()
{
CharacterController controller = GetComponent<CharacterController>();
moveDirection = new Vector3(OVRInput.Get(OVRInput.Axis2D.PrimaryThumbstick).y, 0, OVRInput.Get(OVRInput.Axis2D.PrimaryThumbstick).x * (-1));
moveDirection *= speed;
// moveDirection = transform.InverseTransformDirection(moveDirection);
if (controller.isGrounded)
{
if (OVRInput.GetDown(OVRInput.Button.PrimaryThumbstick))
{
moveDirection.y = jumpSpeed;
}
}
moveDirection.y -= gravity* 3 * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}