- Home /
First Person Camera switch to Third Person Camera on Object
I have a first person exploration game that i'm working on. I am not to familiar on how to code, but I do generally understand how to read it after going through a few dozen tutorials. My question is, how would I go about walking up to a spaceship whilst in a first person view and hitting a key next to the spaceship to enter the spaceship in a third person view? I would need some script telling my first camera to disappear and the engage my second one, right? I am so confused, please enlighten me! :D
Answer by Seth-Bergman · May 01, 2012 at 04:56 AM
you could do it that way. If you create variables for each camera, you can toggle them like:
var camera1 : Camera;
var camera2 : Camera;
camera1.enabled = false;
camera2.enabled = true;
(normally your primary camera is tagged "Main Camera", and could be accessed from anywhere with Camera.main, as in: Camera.main.enabled = true; just FYI)
OR, you could simply zoom out and raise up a bit, probably a bit more complex for your position. To zoom out with a perspective camera, you would say:
Camera.main.fieldOfView = Mathf.Lerp(Camera.main.fieldOfView,targetZoom,Time.deltaTime * speed);
(where targetZoom and speed are floats)
EDIT:
to flesh it out a bit
var ship : GameObject;
var player : GameObject;
var getInShipDistance : float = 1;
var camera1 : Camera;
var camera2 : Camera;
function Start()
{
ship = GameObject.FindWithTag("ship");
player = GameObject.FindWithTag("player");
var tempCamera = GameObject.FindWithTag("camera1");
camera1 = tempCamera.GetComponent(Camera);
tempCamera = GameObject.FindWithTag("camera2");
camera2 = tempCamera.GetComponent(Camera);
}
function Update()
{
var distance = Vector3.Distance(ship.transform.position,player.transform.position);
if (distance <= getInShipDistance)
{
camera1.enabled = false;
camera2.enabled = true;
//code to start animation etc for getting in ship
}
}
of course, this script would probably be attached to the player (or one of the other referenced objects), so you could eliminate that variable. For example, if it were attached to the player, you wouldn't need the player variable. The line for distance would instead simply be:
var distance = Vector3.Distance(ship.transform.position,transform.position);
Answer by hollisb · May 01, 2012 at 07:11 PM
Alright, thank you so much. I have a feeling this would work, now I just need to figure out how and where to implement this into my game. Do I make a new script and attach it to both of my cameras? Thank you again for helping!
if you were to use the above,you could attach it to any object in the scene, and you would also need to create a tag for each object involved.. as in the start function("camera1","player", etc.). Or, once you've attached the script to something (let's say the player), then when you select that player, the script will become visible in the inspector. You can then drag and drop the appropriate object from the hierarchy panel into each slot.. for example, the main camera in the scene would go into the slot "camera1", the finish camera to "camera2" etc... that would be enough, I think, to trigger the camera switch. If as you said you want it on button press, you can replace the line :
if (distance
if (distance
oh yeah, try to use the comment field to respond to an answer, rather than log it as a new answer
Your answer
Follow this Question
Related Questions
Mouselook in space, without a 'down' or 'up' 1 Answer
How to make two cameras face the same direction? 1 Answer
How do i combine first and third person views? 1 Answer
Transition between 1st person and 3rd person cameras 2 Answers
Would anyone mind telling me about memory size of blank camera built in APK? 0 Answers