- Home /
Changing character model in game
Hi there, What I am trying to do is change the character model during the game. For example I want to switch between a rock and a tree when a collision occurs. Does anyone know an easy way of doing this? Thanks.
Answer by LucasMars · Jul 21, 2014 at 03:55 PM
private Vector3 vectorforobject;
public GameObject prefab;
void OnTriggerEnter(Collider c){
if(c.tag=="objtoreplace"){
vectorforobject = c.transform.position;
Instantiate(prefab, vectorforobject, Quaternion.identity);
Destroy(c.gameObject);
}
}
What is happening is when you collide with the tree, it creates another object in place. Then it destroys the original object.
Set the object you want to replace as Is Trigger, set the object to replace tag to "objtoreplace" and place this script on the player.
Edit - Replaced the script as it was not working.
Awesome! But is there anyway you or anyone else could translate that into javascript because without it I dont understand how to alter the end prefab. Thanks
private var vectorforobject : Vector3;
var prefab : GameObject;
function OnTriggerEnter(c : Collider){
if(c.tag=="objtoreplace"){
vectorforobject = c.transform.position;
Instantiate(prefab, vectorforobject, Quaternion.identity);
Destroy(c.gameObject);
}
}
To change the object it turns into, in the editor, the script should have a place to put a GameObject.
If I answered your question, please set the answer as correct.