Character swap out.

Okay, I’m fairly noobish when it comes to scripting. But I’m trudging forward anyway, and could use a fair amount of help with what I assume is a simple issue.

the character from my game is able to shift his form from one thing to another. Now, I need a script that will, at the touch of a button, change him into his new form.

So, if someone could point me in the right direction it would be greatly appreciated. To summarize, the script needs to do the following after pressing one button:

-Stop the control of the character.
-Play an animation of him transforming.
-At the end of the the animation, swap out Character Model A with Character Model B at the exact same position.
-Apply a different control script to the newly transformed Model B.
-Re-enable controls.

Now, this sounds like a lot, but I think it’s fairly simple. So if anyone could help out, it’d be greatly appreciated. Thanks in advance!

Bump.

Just to clarify, I’m not looking for a script-handout. Just hoping someone can tell me what kinds of actions I should be going through to make this happen.

It might be easiest to have separate object for each form the player can take and control each with its own script. It is straightforward to create a new object at the character’s current position (see Object.Instantiate) and destroy the original (see Object.Destroy). Depending on how you are controlling your character, you might need to add a behaviour state to handle the transition animation or you might be able to use a coroutine for this.

Thanks, Andee, that was a wealth of information!

I tried writing a script based on your tips, but I’ve run into a wall.

var state = 0;
animation["BallChange"].layer = 11;
animation["BallChange"].wrapMode = WrapMode.Once;

function Update()
{
if(Input.GetKey("f")  state == 0){
ChangeToBall();
}

if(Input.GetKey("f")  state == 3){
ChangeBack();
}
}

function ChangeToBall(){
if (state == 0){
animation.Play["BallChange"];
WaitForSeconds(.25);
state = 1;
return;
}
if (state == 1){
Destroy(GameObject);
state = 2;
return;
}
if (state == 2){
Instantiate (prefab);
}
state = 3;
return;
}

function ChangeBack(){
if (state == 3){
animation.Play["changeback"];
WaitForSeconds(.25);
state = 0;
return;
}
}

So, I keep getting the error “Array index is out of range”. And I’m just not sure what that means.

Any idea what I’m doing wrong? I believe it has to do with the animation.Play.

Thanks!

//not a scripter, just a suggestion//

Hi,

Have you looked into the robot in the first person shooter tutorial? If it dies it replaces the robot with the same unit only that one has no AI and is a ragdoll.

The index-out-of-range error is happening because you are using square brackets with animation.Play. This is actually a function call, so you need ordinary brackets. The error message is a bit misleading here, but it’s as if you are treating animation.Play as an array (which is what the square brackets mean).

Okay, you guys have been very helpful so far. But I’ve run into one more problem.

var state = 0;
animation["BallChange"].layer = 11;
animation["BallChange"].wrapMode = WrapMode.Once;
var Skratchball : GameObject;
private var player : Transform;
var SkratchCharacter : GameObject;


function Update()
{
if(Input.GetKey("f")  state == 0){
ChangeToBall();
}

if(Input.GetKey("f")  state == 3){
ChangeBack();
}
}

function ChangeToBall(){
if (state == 0){
animation.Play("BallChange");
WaitForSeconds(1);
state = 1;
}
if (state == 1){
Destroy(SkratchCharacter);
state = 2;
}
if (state == 2){
Instantiate (Skratchball, transform.position, transform.rotation);
state = 3;
return;
}
}

function ChangeBack(){
if (state == 3){
Destroy(Skratchball);
state = 4;
return;
}
if (state == 4){
Instantiate(SkratchCharacter, transform.position, transform.rotation);
state = 5;
return;
}
if (state == 5){
animation.Play("ChangeBack");
state = 0;
return;
}
}

Now, the problem I have is that it doesn’t wait for the animation “BallChange” to finish playing before it destroys the character. How do I get it to wait for the animation to finish? Also, it doesn’t seem to take into account when there is a “WaitForSeconds” command. What am I missing here?

Lastly, what do I do if I need the Instanced object to have a different rotation. For example, what if I need the instanced object to be rotated an extra 90 degrees? how would I go about fixing that?

Any help is greatly appreciated!

Ahh, nvm, I solved both of those problems. Thanks for everything!

I think you’ll like using Switch statements rather than If/Else

Switch Statements