x


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

more ▼

asked May 01 '12 at 04:17 AM

hollisb gravatar image

hollisb
0 1 1 1

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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);

more ▼

answered May 01 '12 at 04:56 AM

Seth Bergman gravatar image

Seth Bergman
7k 10 16 28

(comments are locked)
10|3000 characters needed characters left

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!

more ▼

answered May 01 '12 at 07:11 PM

hollisb gravatar image

hollisb
0 1 1 1

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 <= getInShipDistance) with

if (distance <= getInShipDistance && Input.GetKeyDown("space"))

oh yeah, try to use the comment field to respond to an answer, rather than log it as a new answer

May 01 '12 at 10:45 PM Seth Bergman
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3000
x181
x90
x52
x49

asked: May 01 '12 at 04:17 AM

Seen: 886 times

Last Updated: May 01 '12 at 10:46 PM