How do I copy the rotation values of an object to another in one script?

(Posting here because I can’t post in Getting Started for some reason) I have these 2 cameras, and when I hold down “c” I want one camera to have the same rotation values as the other one, and vice versa for when the “c” stops being pressed. This is because one camera will be moving and rotating in a different direction than the other when “c” is being pressed or not. I want them to be in sync, so when I press c it doesn’t look like my camera changed, though its a different one. I’ve got the moving part sorted out (the movement part is in other script), but I’m having problems with the rotation part as it keeps giving me this error message:

Here’s the script:

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

public class holacopia : MonoBehaviour
{
	Vector3 CameraOriginal;
	Vector3 Camera1Original;


	public GameObject Camera;
	public GameObject Camera1;
	


	void Update(){
		CameraOriginal = new Vector3(Camera.transform.rotation.x, Camera.transform.rotation.y, Camera.transform.rotation.z);
		Camera1Original = new Vector3(Camera1.transform.rotation.x, Camera1.transform.rotation.y, Camera1.transform.rotation.z);


		if (Input.GetKeyDown(KeyCode.C)){
			Camera1.transform.rotation = CameraOriginal;
		}
		if(Input.GetKeyUp(KeyCode.C)){
			Camera.transform.rotation = Camera1Original;

		}
	}
}

Anyone knows what’s the problem? (I’m not very experienced in coding.)

much as the quarternion looks like a vector 3, eg it has 3 parts, it is not a vector3 which is why it says it cant convert them. Change your vector3 to quarternions, or just dont, and where necessary set camera1.transform.rotation to be camera.transform.rotation, and visa versa

1 Like

Thanks for the answer, but how do I exactly change a Vector3 to a Quartenion? After searching up some documentantion on it, it’s been getting me very confused and can’t do it still.

Click the cursor on the word, delete it, type in the new one.

Wait wait wait.
The issue your having is that you absolutely don’t understand what you’re doing or how this works.

First, learn what data types in programming are. You cannot mix and match between them, because apples and oranges. There is usually some conversion required, but to convert between any two types you need to appreciate the meaning between the two.

Second, rotations in Unity are described with quaternions, and not vector3s. No, you cannot treat quaternion as plain sequences of numbers, like you would with vector3s. Just a cursory glance on the docs will reveal numerous warning signs that quaternions aren’t to be trifled with. These are tight, mathematical constructs whose numbers aren’t human-readable and you can’t just extract them willy-nilly and expect that’s it.

Third, you can easily extract a quaternion from any transform and assign it to any other transform (or variable).

Quaternion savedRotation = myObject.transform.rotation;
myOtherObject.transform.rotation = savedRotation;

Isnt that what i explained?

My code looks like this now and it still doesn’t work.

public GameObject Camera;
public GameObject Camera1;


void Update()
{

	if (Input.GetKeyDown(KeyCode.C))
	{
		Quaternion savedRotation = Camera.transform.rotation;
		Camera1.transform.rotation = savedRotation;
	}
	if (Input.GetKeyUp(KeyCode.C)) 
	{
		Quaternion savedRotation = Camera1.transform.rotation;
		Camera.transform.rotation = savedRotation;

	}
}
}

And yeah I may have no idea what I’m doing.

Well actually it doesn’t give me the error now but it’s just as if the code wasn’t running at all. Like it does nothing.

Add some debugging statements so you can see whats going on