- Home /
Character Change Within Level
I'm sure you guys know of this in games, where a player's character is in a beat streak fighting enemies and then is able to enter a rage mode or something.
What I need to figure out is this: I have a character that I want to change form when the meter is full. The concept would be changing one character prefab into another and have the meter on a time based decrease. When the meter is empty I want the player to revert back to the first form. The two character prefabs have a separate armature, build, and animations. I want to avoid parenting the second character prefab to the first one because I had issues with the character controller doing that.
How would I go about doing this?
Answer by DragonFang17 · Dec 03, 2017 at 03:12 PM
This is what I was able to come up with and it works fine:
public Rigidbody nextForm;
Rigidbody instantiatedNextForm;
public GameObject tempTeleport;
public Transform player;
I have a separate game object away from the level platform out of view from the player and named/tagged it tempTeleport
void SetTeleport() {
tempTeleport = GameObject.FindWithTag ("tempTeleport");
}
I have the instantiated second prefab spawn in to the first prefab's position and rotation after the meter is filled. Then right after I had the first prefab change it's transform to the tempTeleport game object's position with the controls disabled.
instantiatedNextForm = Instantiate (nextForm, player.position, player.rotation) as Rigidbody;
Vector3 playerPosition = (player.position = tempTeleport.transform.position);
GameObject.Find ("Form1");
if (GameObject.Find ("Form1") != null) {
GameObject.Find ("Form1").GetComponent<CharacterControlForm1> ().enabled = false;
}
There's a separate camera parented to the second prefab so I didn't have to worry about the view messing up. Any newly added camera's in the game scene just overlap each other.
The meter then decreases and the second prefab has it's controls. When it runs out, the first player prefab's transform goes right back to the second one, I have the second prefab destroyed, and enable the first prefab's controls, and start it all over again when the meter is filled.
Vector3 playerPosition = (player.transform.position = instantiatedNextForm.transform.position);
Destroy (instantiatedNextForm.gameObject);
GameObject.Find ("Form1");
if (GameObject.Find ("Form1") != null) {
GameObject.Find ("Form1").GetComponent<CharacterControlForm1> ().enabled = true;
}
I dislike answering my own questions, but I'm self taught with programming. I will eventually solve it.. Thank you all who put in their answers. It's very much appreciated! :)
Answer by FM-Productions · May 27, 2017 at 03:12 PM
Hi,
how about having both characters instantiated in the scene and set them to active/inactive as you need. gameObject.setActive(). Of course, both character prefabs would have to be able to react to user input. Another way I can think of is to have an InputManager and you call certain actions on either your first or second character, based on which one is active. You do not have to deactivate a character gameobject. maybe it is enough to move them out of the cameraview.
Another possibility is to have them controlled both at the same time (both have the same root transform, so the always are in the same place) but render only one of them. Collisions against other characters should only be performed with the visible character.
@Eicher But having both of those prefabs together that have colliders would be messy, one would knock the other out of place and controlling both at the same time would make one of them wonder off away from the previous one @_@
@DragonFang17 Couldn't you just make a script to temporarily disable the collider of the unrendered prefab? Here is an example code that may help:
var YourGameObject : GameObject
var Collider:Collider=Collider.GetComponent("$$anonymous$$esh Collider");
if(YourGameObject.Renderer.isVisible) Collider.enabled = !Collider.enabled;
(The code is untested, so the if statement may not work, but this is just an example.)
When an object is inactive, all its components are also inactive (this includes colliders). You don't need any special script for the inactive object's colliders to be ignored.
I'm still puzzled on how I can have this feature in my game :/
Okay, what about parenting a prefab that was spawned in through instantiation, how would I parent the first playable game object to the second spawned in one?
Your answer