Switching player's controls prefab

Hi, all,

Any ideas how to switch out a third person controlled
object with another via a trigger?

Eg. Turning a player controlled sphere into a player controlled cube (with different controls) from a pickup item.

Thanks,

Greg

Have you tried to just dissable the sphere third person controller script and enabling the cube’s? That should do the trick.

Thanks, but I’m not sure how to do that. Also, if I just enable the cube’s controller script how can I get the cube’s prefab to appear along with it?

Thanks,

Greg

Well say you are making a game where you can drive vehicles.

Both your player and say tank would have a third person controller, but if both are enabled then they’d move at the same time.

So what you want is to have the tank character controller dissabled and when the player enters the vehicle enable the tank controller and dissable the player’s.

To have the player move along with the tank you can set the tank as the player’s parent, or you could also use a fixed joint.

To enable or dissable a script you need to get an instance to the script like:

var myTankController = tankTransform.GetComponent("ThirdPersonController");

Where tankTransform is the transform where the tank’s third person controller script is attached to.

Then you can just to

myTankController.enabled = true;

or

myTankController.enabled = false;

The same goes with the player.

Ratamorph, thanks for you help! I’m beginning to understand, but have a question:

How do I eliminate the original prefab that I was controlling instead of moving it along with the new instantiated object?

Thanks for you time!

Greg

Destroy(instanceToDestroy);

You could try:

function OnTriggerEnter(colliderHit : Collider)
{
  Instatiate(prefabToInstatiate, transform.position, transform.rotation);
  Destroy(transform.gameObject);
}

That code will instatiate the prefab you want and destroy the object this script is on. Assuming this script is in the object you are controlling prior to touching the pickup and the prefab you instatiate has the third person controller script activated it should do something similar to what you want.

you could also do:

function OnTriggerEnter(colliderHit : Collider)
{
  Instatiate(prefabToInstatiate, transform.position, transform.rotation);
  Destroy(colliderHit.gameObject);
}

that one destroys the object it touches and spawns what you want, this you can attach to the powerup instead of the object you are controlling prior. Beware the script will spawn the prefab whenever anything enters its trigger, but you can test against, tag, layer or name befor you instantiate to prevent that from happening.

Also you can add:

Destroy(transform.gameObject);

At the end to destroy the powerup.

That did it!! You’re awesome! Thanks soooo much for your time and help, Ratamorph. It’s knowledgeable guys like you that make this forum so outstanding.

Thanks,

Greg

Thanks, just trying to help like others did when I needed.