- Home /
Teleporting a camera to a camera
Basically, I want a camera to teleport to the exact same position and rotation as another camera. How can I do this in JavaScript?
I have function Update that checks for when a player presses a certain key, and when that key is pressed, I want the camera to teleport to the main camera.
Answer by robertbu · Mar 02, 2014 at 05:17 AM
I think you just want:
transform.position = Camera.main.transform.position;
transform.rotation = Camera.main.transform.rotation;
This assumes you've left the main camera tag as 'MainCamera'.
They're both tagged as main camera though. I have two variables in the script for both of the cameras(cam1 and cam2), so is there a way to use that but it uses the variables ins$$anonymous$$d?
Sure. I'm not sure which camera is which, but let's assume that this script wants to move to the position of cam1:
transform.position = cam1.transform.position;
transform.rotation = cam1.transform.rotation;
This script isn't attached to either of the cameras. It's on a pause menu script. "cam2" is a camera that doesn't move, so when the game pauses, "cam2" will become the active camera. "cam1" is normally the main camera. So would I write the code like: cam2.transform.position = cam1.transform.position;
You are going to have to get access to the transform of the two cameras somehow. Assu$$anonymous$$g these camera's exist all the time in the scene, the easiest way would be to put two variables in the top of your pause script:
var cam1 : Transform;
var cam2 : Transform;
You would initialize these two variables selecting the game object with the pause script in the hierarchy and by dragging and dropping each camera object from the hierarchy on the appropriate variable in the Inspector. Then your code would be:
cam2.position = cam1.position;
cam2.rotation = cam1.rotation;
Another way would be to give each camera a unique name and initialize some variables in start:
private var cam1 : Transform;
private var cam2 : Transform;
function Start() {
cam1 = GameObject.Find("$$anonymous$$ain Camera").transform;
cam2 = GameObject.Find("Other Camera").transform;
}
Then you would move and rotate the camera the same way as the previous solution.
Your answer
Follow this Question
Related Questions
How to create a panning, rotating camera with Cinemachine Freelook camera? 0 Answers
Unity - Dancing Ball World camera following and rotating system 1 Answer
Would anyone teach me how to create camera movement and look controls? 2 Answers
Cinemachine input axis camera movement,cinemachine input axis control with script 1 Answer
How to allow camera to complete an upside down rotation while using LookAt() 0 Answers