- Home /
Instantiate new third person controller at the exact point first third person controller is destroyed
Hallo, In my game my third person controller is destroyed when he hits a box collider, and a new one instantiated in his place.
At the moment the prefab is instantiated in the middle of the box collider, but I was wondering if there is a way of instantiating the new third person controller at the exact point the first third person controller is destroyed?
This is the script I'm using to instantiate my new third person controller (JavaScript):
#pragma strict
var prefab : GameObject;
var script : SwitchCharacters;
var vincentCamera : GameObject;
var playerGO : GameObject; //Should be filled with the GameObject the SwitchCharacters script is attached to
private var hasPlayed = false;
function OnTriggerEnter () {
var pos : Vector3;
if (!hasPlayed){
var newprefab = Instantiate (prefab);
var cameraofnewPrefab = Instantiate(vincentCamera);
script = playerGO.GetComponent(SwitchCharacters);
script.player02 = newprefab;
script.cam02 = cameraofnewPrefab;
hasPlayed = true;
}
}
I found a lot on the forums about adding Instantiate(prefab, Vector3(0,0,0),Quaternion.identity);
but I can't work out how to instantiate the character at the exact point where the first character was destroyed.
Any ideas?
Thanks, Laurien
Answer by SubatomicHero · Jun 04, 2013 at 01:48 PM
You need to get the position of the first character before you destroy it by storing it in a varaible. Then when you instantiate your new prefab, you pass in that Vector:
Instantiate(prefab, firstCharacterPosition, Quaternion.identity);
Sorry (very new to ga$$anonymous$$g but hopefully I'll get there) but how do I store the position of the first character in a variable? And won't that be destroyed once the first character is destroyed? Do I have to use rigidbody.position = Vector3.zero
?
No worries Laurien. Before you call the destroy function to destroy your first game object, you create a variable like so:
var firstCharacterPosition : Vector3;
// Then just before you call destroy
firstCharacterPosition = firstCharacter.transform.position // firstCharacter is the name of the gameobject
Destroy(firstCharacter);
@laurienvictoria you can set this answer as true :) that yould help others searchers.
To get the variable from the first script you need to use GetComponent(), if the scripts are attached to the same object then you can use:
var prefab : GameObject;
var characterPosition : Vector3;
private var hasPlayed = false;
function OnTriggerEnter () {
if (!hasPlayed){
characterPosition = GetComponent("ScriptName").firstCharacterPosition;
Instantiate (prefab, characterPosition, Quaternion.identity);
hasPlayed = true;
}
}
where "ScriptName" should be replaced by the name of the first script (where you save the variable)
If it was attached to a different gameobject you have to use GameObject.GetComponent where you will need either a way to find the gameobject, or have that saved as an additional variable.
If the first script was attached to an object that was then deleted I think you will need to look at Static variables (not certain of that!)
Scribe
It's is possible that this is simply because the second script trigger event is being called just prior to the DestroyCharacter script. To your InstantiateNewCharacter script try adding var thirdPerson : GameObject; and change if (!hasPlayed) to if (!hasPlayed && !thirdPerson.active)