• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by hollisb · May 01, 2012 at 04:17 AM · cameraspacethird-personspaceshipfirst-person

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

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image

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

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

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!

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Seth-Bergman · May 01, 2012 at 10:45 PM 0
Share

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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Mouselook in space, without a 'down' or 'up' 1 Answer

Transition between 1st person and 3rd person cameras 2 Answers

How to make two cameras face the same direction? 1 Answer

How do i combine first and third person views? 1 Answer

Advice on my Ship AI... 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges