- Home /
Changing player model with keypress.
Hello,
I am having trouble coming up with the script for when the player presses a key, the current character model will switch to another model.
I am using javascript.
if (Input.GetButton ("1")) {
//player switches character
}
I have the 3 models in the hierarchy but I am not sure how I would call on them to switch when the key is pressed.
Thanks for any help and time taken.
Answer by demize2010 · Mar 29, 2011 at 08:24 PM
I have a feeling that this may get a bit more complicated then you currently suspect if these are animated or moving objects etc... but for what your asking you'll need to destroy/disable the current model and and instantiate/enable the next one. The simplest option being:
currentModel.SetActiveRecursively(false);
newModel.SetActiveRecursively(true);
Thanks for help there.
Yeah I only just started and wasn't really to sure on the difficulty. I intend to have all the models moving. I thought I would just have to attach the movement scripts onto the model but I'll take a further look into it.
Thanks
The way to do it would to have a base gameobject with your movement script and/or rigidbody on it. Then make the models children of that object ;)
Animations are going to get tougher but not too much, if their animations have the same name you just need a current$$anonymous$$odel variable.
If you want to seamlessly blend from one models position to another - eg match up the bone positions - it may get a bit tougher.
Hi I have just done that. Are the models expected to follow the gameObect when parented? I have created a empty gameObject that has two of the models parented to it. Only the gameObject moves along with the $$anonymous$$ainCamera.
Answer by Tommy · Mar 29, 2011 at 08:27 PM
Here's a little script the might work:
var modelOne = GameObject.Find("Player1"); var modelTwo = GameObject.Find("Player2"); var modelThree = GameObject.Find("Player3");
if (Input.GetKeyDown ("1")) { modelOne.SetActiveRecursively(true); modelTwo.SetActiveRecursively(false); modelThree.SetActiveRecursively(false); } else if (Input.GetKeyDown ("2")) { modelOne.SetActiveRecursively(false); modelTwo.SetActiveRecursively(true); modelThree.SetActiveRecursively(false); } else if (Input.GetKeyDown ("3")) { modelOne.SetActiveRecursively(false); modelTwo.SetActiveRecursively(false); modelThree.SetActiveRecursively(true); }
Hope this will help you out!
Hi, thanks for the reply.
Should I put this as a script on it's own? I've tried adding it along with my movement keys but keep getting Unity Exception Input Button 1 is not a setup.
You could make this a script itself, make sure you add the gameobjects to the vars.
Strange, i get the same error... I'll look into it! Edited the code btw.
I have added the scripts now, following demize2010's player organizing, the models are now in a gameObject. The script is attached along with the models dragged onto the scripts but nothing is changing as well as all models are displayed on screen at the same time.
Thanks for the time and help, I've got it working now!
Cheers!
Answer by simjam03 · Aug 22, 2011 at 03:56 PM
Hi Alan just wondering if you could link me to demize2010 player organizing if possible.
cheers