Home > Enterprise >  How can I control two cameras simultaneously in unity?
How can I control two cameras simultaneously in unity?

Time:08-20

So I need to control 2 cameras in the same way, so they need to have the same rotation at the same time. This is my current script and for some reason it only rotates the second camera on the x axis while the y axis remains unrotated.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MouseController : MonoBehaviour
{
public float mouseSensitivity = 100f;

public Transform playerBody;
public Transform secondaryCamera;

float xRotation = 0f;

// Start is called before the first frame update
void Start()
{
    Cursor.lockState = CursorLockMode.Locked;
}

// Update is called once per frame
void Update()
{
     float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
     float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

     xRotation -= mouseY;
     xRotation = Mathf.Clamp(xRotation, -90f, 90f);

     transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
     playerBody.Rotate(Vector3.up * mouseX);

     // secondary camera needs to match primary camera rotation
     secondaryCamera.Rotate(Vector3.up * mouseX);
}
}

CodePudding user response:

It will be much easier and efficient if you simply copy the rotation of the first camera onto the second.

secondaryCamera.rotation = primaryCamera.rotation;
  • Related