- Home /
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);
}
}
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);
Your answer
Follow this Question
Related Questions
How to Make the SmoothFollow Script Follow Faster? 1 Answer
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
Camera focusing on an object specified in a script 1 Answer
How to change Target of camera from Standard Assets 1 Answer