- Home /
How do I use a power up to change the character I'm controlling?
Here is my situation:
I have a particle system with a box collider attached with "is trigger" checked.
I have 2 playable characters with character controllers attached.
When you start the game you start out as "character 1" which has the tag: "Player."
When the character runs through the "powerup" which is the particle system listed above (#1) I want him to change into character #2 which has the tag: "Player 2."
I'm new to Unity3d and I could have this totally wrong... but I have the following script attached to the particle system prefab I created:
var redapple : GameObject;
var yellowapple : GameObject;
function OnTriggerEnter(other : Collider){
if(other.gameObject.tag == "Yellow Powerup"){
Destroy(other.gameObject);
redapple = GameObject.FindWithTag("Player");
yellowapple = GameObject.FindWithTag("Player2");
redapple.active = false;
yellowapple.active = true;
}
}
Any ideas guys? I've been trying to figure this out for so long now... appreciate any help. Thanks!
Answer by Kilometers · Jan 21, 2012 at 05:44 AM
Since I'm primarily a C# guy, I'm not going to make a code fragment to demonstrate this, but it should send you in the right direction. Avoid using Destroy() if you can. It is costly, and you may want to return to the "redapple" object later. Instead, have two (2) GameObjects that hold redapple and yellowapple, and make them the children of a "playercontroller" GameObject. When the level loads, make sure to start things off with redapple.renderer.enabled = true, and yellowapple.renderer.enabled = false. When the playercontroller object enters the trigger, just set redapple's renderer.enabled to false, and the opposite for yellowapple.
I don't know what the apple meshes look like, but if they are the same mesh and only color is changing, you may want to consider sticking with a single "apple" GameObject and simply change the material/texture.
So I did what you said and I created a script for Gamestart to apply the renderer only to the red apple. I also changed the powerup script accordingly. However, when the game starts I'm controlling both apples haha... it's kind of funny. Their meshes are intertwined with each other. When the apple enters the "powerup" the mesh renderer of the parent apple object turns off in the inspector ( I can see it uncheck itself), but the child objects are not affected (the actual red and yellow apples). Here are my scripts... any ideas? Also their meshes are not at all the same, so the idea of just changing color won't work. Each apple has different "abilities" as well.
APPLE START:
var redapple : GameObject;
var yellowapple : GameObject;
function Start () {
redapple = GameObject.FindWithTag("Player");
yellowapple = GameObject.FindWithTag("Player2");
redapple.renderer.enabled = true;
yellowapple.renderer.enabled = false;
}
YELLOW POWERUP:
var redapple : GameObject;
var yellowapple : GameObject;
function Start () {
redapple = GameObject.FindWithTag("Player");
yellowapple = GameObject.FindWithTag("Player2");
redapple.renderer.enabled = true;
yellowapple.renderer.enabled = false;
}
Not sure if I need to declare the variables in both scripts... still new to Unity. The apple start script is attached the parent object containing both apples. The yellow powerup script is attached to the powerup with "is trigger" checked.
Sorry I copied the wrong script for the yellow powerup. Here is the correct one:
var redapple : GameObject;
var yellowapple : GameObject;
function OnTriggerEnter(CharacterController : Collider){
redapple = GameObject.FindWithTag("Player");
yellowapple = GameObject.FindWithTag("Player2");
redapple.renderer.enabled = false;
yellowapple.renderer.enabled = true;
}
Answer by Azound · Jan 21, 2012 at 06:02 AM
It looks like you put a component on the Particle System looking for when it collides with the Particle System... Did you mean to put this on the character instead? Otherwise the Particle System should be looking for a collision with the character.
var redapple : GameObject;
var yellowapple : GameObject;
function OnTriggerEnter(other : Collider){
if(other.gameObject.tag == "Player"){
Destroy(this.gameObject);
redapple = GameObject.FindWithTag("Player");
yellowapple = GameObject.FindWithTag("Player2");
redapple.active = false;
yellowapple.active = true;
}
}
When I did this the game stopped running and said "null reference" or something along those lines. I messed around with the other suggestion from $$anonymous$$ilometers above as well. You can see my revision. I don't know if you have any thoughts regarding this... but any help is appreciated. Thank you! :)
Answer by stingman · Jan 22, 2012 at 12:52 AM
Ok, thanks to the above advice and a lot of research I finally got it working correctly! Here is the code in Java in case anyone else is looking to do this:
Create a script and attach it to the POWERUP:
private var yellowapple : GameObject;
private var redapple: GameObject;
function Awake () {
yellowapple = GameObject.FindWithTag("Player2");
redapple = GameObject.FindWithTag("Player");
yellowapple.SetActiveRecursively(false);
redapple.SetActiveRecursively(true);
}
function OnTriggerEnter(other : Collider){
yellowapple.SetActiveRecursively(true);
redapple.SetActiveRecursively(false);
}
Your answer
Follow this Question
Related Questions
A Character Controller with another shape. 0 Answers
Character Controller giving false collisions 0 Answers
Odd character controller behavior when a shield object parented to child 0 Answers
CharacterController colliding with normal Box Colliders 1 Answer
Determinism of collision judgment of Character Controller 0 Answers