Hello there!
I am currently working on a Character Controller script. My goal is to be able to control player in first-person movement, with Cinemachine virtual camera as the camera.
While my movement script fully works with no issues when you look at the character from a top-down camera, i am having issues with rotating the player together with the rotation of the camera.
My Cinemachine cam setup:
Virtual cam’s Follow is set to an Empty child of Player that is called vcamPosition.
Body: Hard Lock To Target.
Aim: POV
I am using old input system.
Code (Character Controller):
- I declare the camera with this:
private Cinemachine.CinemachineVirtualCameraBase camObject;
- I assign it to an object from the hierarchy like this:
`camObject = GameObject.Find("PlayerCam").GetComponent<Cinemachine.CinemachineVirtualCameraBase>();`
- then in update() function, I call move() function with the following contents :
rotationInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
direction = new Vector3(rotationInput.x, 0.0f, rotationInput.y);
Vector3 camVector = new Vector3(camObject.State.FinalOrientation.x, camObject.State.FinalOrientation.y, camObject.State.FinalOrientation.z);
Debug.Log(camVector);
if (camVector != Vector3.zero)
{
// change the transform somehow????
}
controller.Move(direction * playerSpeed * Time.deltaTime);
yVelocity.y += gravity * Time.deltaTime;
controller.Move(yVelocity * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.Space) && controller.isGrounded)
{
yVelocity.y = Mathf.Sqrt(playerJump * -2f * gravity);
}
I am sure that i need to put something that changes the transform in the if-statement block, but nothing I try works. Could i please get some help with that?