Sometimes you have objects that have inside them lots of “smaller and less complex” like a first person character for example.
lets assume that this character has a first person controller (an object that acts as a container for the others, and it has some scripts), also he has a model mesh with an armature, he also has weapons attached to his hands and lots of other things inside it.
now the situation, we have two characters, a First Person one, and a Third Person one, we need to toggle between them with a button, but we want it to spawn where the player was when he pressed the button, also we need to disable all the actions and aswell hide him when we do that, (we don’t want to walk with our other player and them see ourselves walking out there like it was another character)
How would we fix that? probably the simples way would be disable the First person, and enable the third person in its place and vice versa.
in unity we could do that using GameObject.active = false;
GameObject in this case is our “container” with lots of scripts, meshes, and other cool things the ” .active ” is a class from GameObject in unity, the false is like toggling a light off, and true toggling on.
if we did something like this:
Java Script to change the player
//our game object variables
var firstPersonPlayer : GameObject; var thirdPersonPlayer : GameObject;
// we want to test it all the time since we dont know when the player will hit the button…
function Update (){
if (” toggle character button “)
{
//if the FPS player is active
if (firstPersonPlayer)
{
//activate our third person player
thirdPersonPlayer.active = true;
//move our third person character to the exact place where we are right now (with the First person player)
thirdPersonPlayer.transform.position = firstPersonPlayer.transform.position;
//disable it (to hide and prevent any unwanted action by it)
firstPersonPlayer.active = false;
}
//if the FPS player is not active then our third person probably is!
else
{
//activate our first person player
firstPersonPlayer.active = true;
//move our first person character to the exact place where we are right now (with the third person player)
firstPersonPlayer.transform.position = thirdPersonPlayer.transform.position;
//disable it (to hide and prevent any unwanted action by it)
thirdPersonPlayer.active = false;
}
}
}
that would work if our object wasn’t a complex object (with lots of children)
how can we fix that?
1 – we could disable each of its children making a big and complex code that probably would have bugs, and even when we fixed it we wouldn’t be able to reuse it on another character for example (he could have different weapons, items, models, and bones) with different names etc.
2 – use something called: “ObjectName”.gameObject.SetActiveRecursively(false); // true if we want it to activate again
then what does this do? the same thing as( “GameObjectName”.active = false; ), but for every component of your object so you wont need to do that manually.
Now the explanation of this post, I had to search a little on how to change between my character views since our game would change between cameras, this is the beta version of code, since it don’t gives a smooth change between the cameras but it was what I needed now, and probably there’s more people with same problem so this could help with something.
CharacterChanger.js
var firstPersonChar : GameObject;
var thirdPersonChar : GameObject;
var thirdPersonCamera : GameObject;
var WeaponSounds : AudioClip[];
function Update ()
{
if (Input.GetButtonDown(“Tab”) )
{
if (firstPersonChar.active){
//activates the camera and player object and its childrens
thirdPersonChar.gameObject.SetActiveRecursively(true);
thirdPersonCamera.gameObject.SetActiveRecursively(true);
//move the third person player to the position of the first person player
thirdPersonChar.transform.position = firstPersonChar.transform.position;
thirdPersonChar.transform.rotation = firstPersonChar.transform.rotation;
//deactivates the first person object and its childrens
firstPersonChar.gameObject.SetActiveRecursively(false);
//plays the release weapon sounds
audio.clip = WeaponSounds[0];
audio.Play();
}
else{
//activates the first person object and its childrens
firstPersonChar.gameObject.SetActiveRecursively(true);
//move the first person player to the position of the third person player and its childrens
firstPersonChar.transform.position = thirdPersonChar.transform.position;
firstPersonChar.transform.rotation = thirdPersonChar.transform.rotation;
//deactivates the third person and camera objects and its childrens
thirdPersonChar.gameObject.SetActiveRecursively(false);
thirdPersonCamera.gameObject.SetActiveRecursively(false);
//plays the pickWeapon sound
audio.clip = WeaponSounds[1];
audio.Play();
}
}
}
so here’s my code for changing from my first person character to third person one.
Also have a look at the updated version of the Junkyard (now you can change the character type of view with TAB.
Link: The Junkyard




Good Post!
First Comment xD
I belive that my group will change to Unity in this season, since many people changed. By the way, I hope that we can make the game in time, and i hope the same for your team too, u have a great idea in hands.