Third person movement with fixed cameras
Hello, I am using multiple cameras and I set the controller to move relative to the camera. The problem occurs once I cut to the next camera, the player changes the movement relative to the new camera. So let's say I hold "W" and I move towards the camera. Once I cut to the new camera, I start walking to the opossite direction because "W" acts as "S" from the new angle.
Here is the part of my code:
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y <0)
{
velocity.y = -2f;
}
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 movementX = Camera.main.transform.right * horizontal;
Vector3 movementZ = Camera.main.transform.forward * vertical;
Vector3 direction = (movementX + movementZ).normalized;
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
controller.Move(direction * speed * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
Answer by AbandonedCrypt · Feb 12, 2021 at 10:06 AM
Camera.main
always refers to the currently active camera. You are making your movement camera-viewaxis-oriented with these lines of code:
Vector3 movementX = Camera.main.transform.right * horizontal;
Vector3 movementZ = Camera.main.transform.forward * vertical;
If you want your movement to be aligned to the view axis of your first camera, create a reference to that camera and use that reference instead of Camera.main
Your answer
Follow this Question
Related Questions
How to have a child object camera with gaze move its parent object 0 Answers
How do I make the movement relative to the camera? 1 Answer
My character walks slower when looking down? 1 Answer
How to make an object go the direction it is facing? (Im new) 0 Answers
Face direction of a Vector 3 1 Answer