• 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 Orloffyeah · Jan 21, 2013 at 03:33 AM · cameracarchangefollowtarget

Changing Target Depending On Car Selected

Hi, what I'm about to ask is a bit complicated so I hope someone understands. I have made a car selection screen, and when a car is selected it is the one that appears on the level, but I have it set up so that the main camera follows a certain game object inside the car, but if in the car selection screen I choose a different car than the one that has the camera following it, then I can't see what is happening on the game. How can I set it up so that when I choose a car the main camera changes its target?

Car Selection Script:

 #pragma strict
     //this is the currently selected Player. Also the one that will be saved to PlayerPrefs
     var selectedPlayer : int = 0;
     
     function Start()
     {
         Camera.main.transform.position = Vector3(2,1.5,-2.5);
     }
     
     function Update() 
     { 
     if (Input.GetMouseButtonUp (0)) {
         var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         var hit : RaycastHit;
         
         if (Physics.Raycast (ray, hit, 100))
             {
                     
                     if(hit.collider.name == "Comodo") 
                     SelectedCharacter1(); //Sends this click down to a function called "SelectedCharacter1(). Which is where all of our stuff happens.
                 
                     if(hit.collider.name == "Vintovka")
                     SelectedCharacter2();
                         
                     if(hit.collider.name == "Player3")
                     SelectedCharacter3();
             } 
             else
             {
             return;               
             }
         } 
     }
     
     function SelectedCharacter1() {
         selectedPlayer = 1;
         PlayerPrefs.SetInt("selectedPlayer", (selectedPlayer));
     }
     
     function SelectedCharacter2() {
         selectedPlayer = 2;
         PlayerPrefs.SetInt("selectedPlayer", (selectedPlayer));
     }
     
     function SelectedCharacter3() {
         selectedPlayer = 3;
         PlayerPrefs.SetInt("selectedPlayer", (selectedPlayer));
     }


Car Spawner Script :

 #pragma strict
 //these are the player prefabs that will be automagically plugged in for us.
 var player01Prefab : GameObject;
 var player02Prefab : GameObject;
 var player03Prefab : GameObject;
 
 //this is where the script placed in the level inputs in this number for the player who was selected
 //and saved by playerPrefs
 var savedPlayer : int = 0;
 
 //this is called first before the Start function, so make sure it loads everthing needed first.
 function Awake() {
 
     // Let's grab the saved data for each player and grab that integer to use to load that player in the world
     savedPlayer = PlayerPrefs.GetInt("selectedPlayer");
         
     player01Prefab = GameObject.Find("Comodo");
     player02Prefab = GameObject.Find("Vintovka");
     player03Prefab = GameObject.Find("Player3");
     
     if(savedPlayer == 0) //if we've not selected any player initially lets just use Player 1 
         {                              
         player01Prefab.SetActiveRecursively(true);
         player02Prefab.SetActiveRecursively(false);
         player03Prefab.SetActiveRecursively(false);
         }
     else if(savedPlayer == 1) //if we've set the player to 1 from playerprefs then 
         {                      
         player01Prefab.SetActiveRecursively(true);
         player02Prefab.SetActiveRecursively(false);
         player03Prefab.SetActiveRecursively(false);
         
         }
     else if(savedPlayer == 2) //if we've set the player to 2 from playerprefs then 
         {                      
         player02Prefab.SetActiveRecursively(true);
         player01Prefab.SetActiveRecursively(false);
         player03Prefab.SetActiveRecursively(false);
         }
     else if(savedPlayer == 3) //if we've set the player to 3 from playerprefs then 
         {                      
         player03Prefab.SetActiveRecursively(true);
         player01Prefab.SetActiveRecursively(false);
         player02Prefab.SetActiveRecursively(false);
         
         }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by sparkzbarca · Jan 21, 2013 at 04:03 AM

i assume you made it follow it by making it a child right? when you select the vintovka

target = gameobject.find("Vintovka_camera_target"); start_pos = camera local position for the vintovka; when you select the comodo

target = gameobject.find("comodo_camera_target") start_pos = local position for comodo;

at the very end simply go

camera.transform.parent = target; camera.transform.localposition = start_pos;

remember you want the local position, the position of the camera relative to the car not to the world. that way no matter where you spawn the car the camera starts at the right place.

basically set the camera in the editor where you want it for the comodo for example

now just debug.log(camera.transform.position - comodo.transform.position) that will give you the local position. the position of the camera if the comodo was at 0,0,0 which is it if the camera is the child.

so just take that position and simply hard code it in

if it pops back (1.2,1,10)

static var comodo_start_pos = new vector3(1.2,1,10);

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how can i make the main cam smooth follow a new prefab that i add after the game start . 5 Answers

Changing camera target of CarSmoothFollow ( Js to c#) 0 Answers

(C#) Follow target (script) transform target PREFAB 0 Answers

How to change Target of camera from Standard Assets 1 Answer

Camera focusing on an object specified in a script 1 Answer


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